Thread: Remove upper case letters from a char string

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Remove upper case letters from a char string

    I am very new to C, so please bear with me.

    I have written the following function...

    Code:
    const char *RemoveUpper(unsigned char *text)
    {
            int i = 0, len = strlen(text);
            static unsigned char new_str[4096];
            while (len > 0)
            {
                    if (*text == toupper(*text)) {
                       new_str[i] = *text;
                    }
                    i++;
                    text++;
                    len--;
            }
            new_str[i] = 0;
            return new_str;
    }
    It is supposed to loop through every character of the variable text and check to see if it is upper case, by comparing it with the uppercase equivialant of it.

    It seems to only return a single character.

    What am I doing wrong here?

    Thanks

    ~Phil~

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by lavinpj1
    It seems to only return a single character.

    What am I doing wrong here?
    What are you passing as input to the function?



    Quote Originally Posted by lavinpj1
    It is supposed to loop through every character of the variable text and check to see if it is upper case, by comparing it with the uppercase equivialant of it.
    Then a better name for the function would be helpfull, RemoveUpper seems to indicate a different purpose.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Code:
    Then a better name for the function would be helpfull, RemoveUpper seems to indicate a different purpose.
    It loops through every character in the input and check if it is uppercase. If it isn't, then it sticks the character to the end of a new char variable, therefore removing upper case letters.

    What I think you may have been confused with is, if (*text == toupper(*text)) {. It is supposed to be if (*text != toupper(*text)) {. My mistake.

    This is part of a module for unreal ircd. It is passed like this...

    Code:
    		
    strncpyzt(retbuf, RemoveUpper(text), sizeof(retbuf));
    in order to set retbuf = to the string inputted, but with the caps removed.

    Cheers

    Phil

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by lavinpj1
    It loops through every character in the input and check if it is uppercase. If it isn't, then it sticks the character to the end of a new char variable, therefore removing upper case letters.
    Is this what you are saying?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char *RemoveUpper(const char *text)
    {
       static char new_str[4096];
       int i = 0;
       for ( ; *text; ++text )
       {
          if ( !isupper(*text) )
          {
             new_str[i++] = *text;
          }
       }
       new_str[i] = 0;
       return new_str;
    }
    
    int main(void)
    {
       char text[] = "Hello World";
       puts(text);
       puts(RemoveUpper(text));
       return 0;
    }
    
    /* my output
    Hello World
    ello orld
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Ah, thanks very much Dave.

    That worked great, but what I did want as islower, not isupper. The only problem I am having, is, that like all programming languages, it thinks characters are uppercase. This is what I want, although I do not want to remove spaces. How would I go about stopping it removing spaces?

    EDIT: Tried...

    Code:
    if ( ( islower(*text) ) && ( *text != ' ' ) )
          {
             new_str[i++] = *text;
          }
    with no luck.

    Cheers

    ~Phil~
    Last edited by lavinpj1; 05-01-2006 at 11:50 AM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by lavinpj1
    The only problem I am having, is, that like all programming languages, it thinks characters are uppercase.
    Quote Originally Posted by lavinpj1
    This is what I want, although I do not want to remove spaces. How would I go about stopping it removing spaces?
    You want to keep only lowercase or spaces?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char *RemoveUpper(const char *text)
    {
       static char new_str[4096];
       int i = 0;
       for ( ; *text; ++text )
       {
          if ( islower(*text) || isspace(*text) )
          {
             new_str[i++] = *text;
          }
       }
       new_str[i] = 0;
       return new_str;
    }
    
    int main(void)
    {
       char text[] = "The Quick Brown Fox Jumps Over The Lazy Dog.";
       puts(text);
       puts(RemoveUpper(text));
       return 0;
    }
    
    /* my output
    The Quick Brown Fox Jumps Over The Lazy Dog.
    he uick rown ox umps ver he azy og
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Perfect! Thanks so much Dave. I would quite like a list of these string functions if you have one, as there are some real nice ones.

    Cheers

    ~Phil~

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Bookmarked!

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM