Thread: Converting a String to Uppercase

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Converting a String to Uppercase

    I've searched the forums, and implemented a lot of answers found here, but none of them seem to work entirely.

    Here is the code in question:
    Code:
    #include <stdio.h>
    #include <strings.h>
    #include <string.h>
    #include <ctype.h>
    int main(int argc, char** argv)
    {
      char buf[10];
      scanf("%s", &buf);
    
      int i;
      for(i = 0; buf[i] != '\0'; i++) {
        if(buf[i] >= 'a' && buf[i] <= 'z')
          buf[i] = toupper(buf[i]);
      }
    
      printf("%s\n",buf);
    
    }
    When I run this and type "this is a test" the printout is "THIS". It only converts the first word of the string. I have tried different variations for the terminating condition of the for loop, but none of them seem to work.

    Other ways I've tried terminating the for loop:
    for( i = 0; buf[ i ]; i++ )
    for( i = 0; i < 10; i++ )

    Other way I tried changing the case of the char's:
    buf[ i ] -= 32;

    It seems to be changing the case of the char as it should, but it just stops working on the string after the first word.

    This is just a sample program, it is actually part of a project where I have a client program and a server program, and I'm sending a message to the server, where it is changed to all uppercase, then sent back to the client. This "toupper" part of the program is the only part not working. Which is why I'm working on it in a separate little test program.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf is only reading the first "word" because that's what %s does.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use fgets to read the whole line
    and islower to check the case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Better yet, don't even bother with a check, because toupper won't "up-case" things that can't be up-cased.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    scanf was the problem, thank you. Using fgets is now getting the whole line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM