string converting upper/lower

This is a discussion on string converting upper/lower within the C Programming forums, part of the General Programming Boards category; I have a problem. I have to accept input and convert it to lower case if it is upper case, ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    string converting upper/lower

    I have a problem. I have to accept input and convert it to lower case if it is upper case, and vice versa. If i enter "HarrY" i should get "hARRy".
    My code stops for some reason after a string has been entered. Can somebody please help me fix it up so that i can get it working. Here is what i have so far, as u can see i am a bit stumped with the reverseCase function:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define STRINGSIZE 100
    
    void getString(char *string);
    void reverseCase(char *string);
    int numGraph(char *string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
    
       getString(string);
       printf("Entered string => %s", string);
    
       count = numGraph(string);
       printf("Number of graphic characters in string = %d\n", count);
    
       reverseCase(string);
       printf("Reverse case   => %s");
    
       return 0;
    }
    
    void reverseCase(char *string)
    {
       /*converts uppercase into lowercase and lowercase into uppercase */
    }
    
    int numGraph(char *string)
    {
       /* Calculate the number of printable graphic characters in the
          string.
       */
    
       int count;
    
       while (string != '\0')
       {
          if (isgraph((int) *string)) count++;
       }
       return count; 
    }
    
    void getString(char *string)
    {
       printf("Please enter a string to process\n");
       fgets(string, STRINGSIZE, stdin);
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a correct version of numGraph()
    Code:
    #include <ctype.h>
    
    int numGraph(char *mystring)
    {
        /* Calculate the number of printable graphic characters in the mystring. */
        int    count = 0;
    
        while (*mystring != '\0')
        {
            if (isgraph((int) *mystring)) count++;
            mystring++;
        }
    
        return count;
    }
    Note that count is initialised to 0 (otherwise you don't know what its value is going to be. We dereference mystring on the while statement to get at the characters its pointing to, and we increment it at the end of the loop.

    To write the reverseCase() function, try use the following:
    islower()
    isupper()
    tolower()
    toupper()

    [edit]
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    This isn't guarenteed to work in all cases, so best stick with the standard functions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    What exactly does this mean?

    And when i use those islower function, isupper, how do i actually get to the individual string elements? Do i have to use some sort of a loop?

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    What exactly does thie mean?

    Also, how do i go about getting to each individual character in the string. Should i use a loop?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    What exactly does thie mean?

    Also, how do i go about getting to each individual character in the string. Should i use a loop?

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    What exactly does thie mean?

    Also, how do i go about getting to each individual character in the string. Should i use a loop?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by jlamn
    >>To convert case you have to add or subtract the difference between the ASCII values of the upper and lower case letters.
    What exactly does thie mean?

    Also, how do i go about getting to each individual character in the string. Should i use a loop?
    Just use 'toupper' or 'tolower', in the header ctype.h.

    You can use a loop. Or you could use recursion:
    Code:
    void up( char*s )
    {
        if( s && *s != '\0' )
        {
            *s=toupper(*s);
            up(s+1);
        }
    }
    Fun. Fun like the fun you had with the submit reply button...

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

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by quzah
    Just use 'toupper' or 'tolower', in the header ctype.h.

    You can use a loop. Or you could use recursion:
    Code:
    void up( char*s )
    {
        if( s && *s != '\0' )
        {
            *s=toupper(*s);
            up(s+1);
        }
    }
    Fun. Fun like the fun you had with the submit reply button...

    Quzah.
    dont you think recursion has a whole lot overhead for not doing much here?
    Code:
    deleted
    edit: code example deleted because quzah has a good point
    Last edited by moi; 09-24-2002 at 07:14 PM.
    hello, internet!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by moi
    dont you think recursion has a whole lot overhead for not doing much here?
    You missed the entire point of the post. I don't do people's work for them. I tell them how ("You can use a loop.") and then provide an amusing alternative.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21