0

I'm fairly new to C and I've read a few chapters of a C book I have and now I have to make an assignment but I am all confused hope someone can help me out.

I have to read 2 strings from user (char arrays) input with a max length of 100 characters and convert them to capital letters and print them out with a newline \n after each word.

Until now I have this:

int main() {
char chars[100];
int i = 0;
char str1;
char str2;
int j = 0;

scanf("\n %c", str1);
scanf("\n %c", str2);


while (str1[i] != '\0') {

    chars[i] = str1[i];
    toupper(chars[i]);
    printf(chars[i]);
    i++;

}

while (str2[j] != '\0') {

    chars[j] = str2[j];
    toupper(chars[j]);
    printf(chars[j]);
    j++;

}


return 0;
}

after it takes the 2 inputs from user, it says stops running and says run failed.

6
  • 1
    str1 and str2 should be array Commented Oct 8, 2013 at 17:14
  • ill try that... didn't make any difference as it still stops after the input Commented Oct 8, 2013 at 17:18
  • 1
    Your compiler didn't give any warnings, right? Oh wait noez... Commented Oct 8, 2013 at 17:19
  • 2
    @user1842140 Hint: you want to use fgets(). Google its documentation. Stay far away from scanf(). Commented Oct 8, 2013 at 17:20
  • Isn't it possible without fgets() ? Not sure if I am supposed to use it. Commented Oct 8, 2013 at 17:24

3 Answers 3

3

This is your immediate problem:

scanf("\n %c", str1);

This format string tells scanf to read some amount of whitespace followed by a single character, which is to be stored in str1.

@H2CO3 is right: scanf is almost never the right answer for reading input from the user. It performs no bounds checking and is too finicky about the format of user input; one unexpected character will completely confuse it. You are very strongly encouraged to use fgets instead if at all possible:

fgets(str1, 100, stdin);

If you are not allowed to do this, you should ask your professor why not. Seriously.

If you absolutely must use scanf to read input here, you can do it the way @user2479209 described in their answer.

There is another problem with your program, which is that toupper(chars[i]) will not change the value stored in chars[i]. You have to assign the result of toupper back to the array explicitly, e.g.:

chars[i] = toupper(chars[i]);
Sign up to request clarification or add additional context in comments.

Comments

2

change str1 and str2 to array. use %100[^\n] in first two scanf's.It's a regular expression trick to read strings with spaces.read about it.

char str1[101];
char str2[101];
scanf("%100[^\n]",str1);
scanf("%100[^\n]",str2);

This will take in two strings.hit enter after entering the first string to enter the second.

Comments

0

I think that you can also use scanf_s (secure scanf) and collect both strings input by user in one shot. You can also define the array size as a symbolic constant for flexibility, on the top of your program, outside of main and below the #include preprocesor directives.

// Symbolic constants
#define SIZE 101

char str1[SIZE];
char str2[SIZE];
scanf_s("%100s%100s", str1, SIZE, str2, SIZE);

Alternatively:

scanf_s("%100s", str1, SIZE);
scanf_s("%100s", str2, SIZE);

I think that, if you don't want to do #define SIZE 101, you can also do:

scanf_s("%100s", str1, 101);
scanf_s("%100s", str2, 101);

I'm new to C so feel free to improve this answer.

1 Comment

Your own question would be better asked as a separate question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.