Mathiassa
Mathiassa

Reputation: 27

c function, that "uppercasing" a string

This is my target:

input: string with mixed ASCII characters (uppercase, lowercase, numbers, spaces)

output: string with only uppercase characters

I have this:

#include <stdio.h>

void csere(char s[]){
    int i;
    for(i=0; s[i]!='\0'; i++){
        if('a'<=s[i] && s[i]<='z'){
            s[i]-=32;
        }
        printf("%c", s[i]);
    }
}

void main(){
    char s[1];
    scanf("%s", &s);
    csere(s);
}

My problem is:

The function stops at the first 'space' character in the string. I tried to change the s[i] != '\0' in the 'for' part for i < strlen(s) or just for s[i], but I still get the same result.

Example: qwerty --> QWERTY, but qwe rty --> QWE

(smaller problem: The program only accepts strings with length less than 12, if i change the 1 to 0 in main function.)

Thanks for help. Sorry for bad English.

Upvotes: 0

Views: 1256

Answers (2)

Jcl
Jcl

Reputation: 28272

scanf only scans non-whitespace characters with the %s modifier. If you want to read everything on a string you should use fgets with stdin as the third parameter:

fgets(s, sizeof s, stdin);

If you really need to use scanf for homework or something, you should use something like:

scanf("%128[^\n]", s);

Also, take note you are not allocating enough space for the string, the fact that it has not crashed is just pure coincidence... you should allocate the space on your array:

char s[128]; // change 128 for max string size

Actually, the fgets() usage I wrote earlier would only read 1 character (including the terminator string) since you only put 1 character on the array... change the array size and it should work.

You could also just use toupper() on ctype.h, but I guess this is some kind of homework or practice.

Furthermore, if you are allowed to use pointers, this would be a shorter (and probably more performant although that'd have to be tested... compilers are good these days :-) ) way to convert to uppercase (notice though it changes your original char array, and doesn't print it, although that'd be easy to modify/add, I'll leave it to you):

void strupper(char *sptr) {
  while (*sptr) {
    if ((*sptr >= 'a' ) && (*sptr <= 'z')) *sptr -= 32;
    sptr++;
  }
}

Upvotes: 4

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

From scanf

s
Matches a sequence of bytes that are not white-space characters. The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence and a terminating null character code, which shall be added automatically.

This means, with %s, scanf reads a string until it encounters the first white space character. Therefore, your function converts the given string only to the first space.

To the second (smaller) problem, the array s must be large enough for the entire string given. Otherwise, you overwrite the stack space and get undefined behaviour. If you expect larger strings, you must increase the size of s, e.g.

char s[100];

Upvotes: 0

Related Questions