0

This block of code is a simple "convert to uppercase" program but, when I compile, I get the error that is the title above. Any solutions?

char *input = argv[1];
printf("%s\n", input);
char toUpper = ("%s\n",toupper(input));
printf("%s\n", toUpper);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 4
    `char toUpper = ("%s\n",toupper(input));` Whats this supposed to be? – tkausl Oct 06 '21 at 23:42
  • 2
    The argument to `toupper()` must be a single character. But `input` is a pointer to a character, not a character. – Barmar Oct 06 '21 at 23:44
  • It looks like you have your `printf` code mixed in with the assignment to `toUpper`. – Barmar Oct 06 '21 at 23:45
  • Are you trying to convert the whole argument to uppercase, or just the first character? If you want to convert the whole argument, you need to loop over the characters, since `toupper()` just works on one character at a time. – Barmar Oct 06 '21 at 23:47
  • @Barmar I am trying to get the entire argument into a uppercase – Kush Patel Oct 06 '21 at 23:55
  • @tkausl im very new to C so sorry about this, but it is supposed to store the string input as a uppercase version of it – Kush Patel Oct 06 '21 at 23:57
  • 1
    You need to review the difference between strings and chars. – Barmar Oct 07 '21 at 00:01

1 Answers1

-1

toupper() does just one char (int).
You need to do each char...

for (char *x=input, *x, x++) *x = toupper(*x);

That changed the string, so then print as before.

printf("%s\n", input);
9mjb
  • 571
  • 3
  • 10
  • Strictly speaking it should be `toupper((unsigned char)*x)`, see https://stackoverflow.com/questions/21805674/do-i-need-to-cast-to-unsigned-char-before-calling-toupper-tolower-et-al – Nate Eldredge Oct 07 '21 at 01:22
  • @NateEldredge Even stricter, s/b `toupper(*(unsigned char *)x)` to properly handle negative values encoded with the antiquated non-2's complement. – chux - Reinstate Monica Oct 07 '21 at 14:54