Thread: Modifying Code to handle uppercase and special symbols

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    6

    Modifying Code to handle uppercase and special symbols

    My code is currently reads in a string of lower case letters, identifying the occurrence of each letter.

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main()
    {
       char string[256];
       int y = 0, num[26] = {0};
     
       printf("Please enter the required string\n");
       fgets(string, 256, stdin);
    
    
     
       while (string[y] !='\0')
       {
            if (string[y] >= 'a' && string[y] <= 'z' ) 
            num[string[y]-'a']++;
     
          y++;
       }
     
       for ( y = 0 ; y < 26 ; y++ )
       {
            if( num[y] != 0 )
            printf("%c occurs %d times in the entered string.\n",y+'a',num[y]);
       }
     
       return 0;
    }
    My issue is that I want my code to read uppercase and special symbols. showing the occurrence of both.

    I have an idea:
    Code:
    else if(str[x] >= 'a' && str[x] <= 'z');
        
    else if(str[x] >= '0' && str[x] <= '9');
    However I struggle to implement it, any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're just counting the occurrences of letters, then perhaps you should consider a loop that makes all the letters lowercase using "tolower()" before you start counting.

    If by "special characters" you mean digits (as indicated in your idea), check out the "isdigit()" function. In that link, on the "ctype.h" list on the bottom left of the screen, you can see other functions that check types of characters, special and otherwise.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Assuming that strings are long and you know that chars are always 0-127 (ASCII) or 0-256 (extended ASCII), the first thing to do is to get a histogram of the string. That's a separate function.
    Now you want to count characters in each class. So a-z and A-Z will always be in range (assuming ASCII) and 0-9 will always be in range (by C standard). But "special characters" could be anything. Do you include control characters, extended ASCII characters, non-alphabetical extended ASCII characters ?
    Either use the isxxx (isupper, islower, isdigit, isgraph etc) functions in ctype.h, or just build a list of your own, by hand. Then loop over the histogram, checking on the index and summing if positive.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Malcolm McLean View Post
    Assuming that strings are long and you know that chars are always 0-127 (ASCII) or 0-256 (extended ASCII), the first thing to do is to get a histogram of the string. That's a separate function.
    Now you want to count characters in each class. So a-z and A-Z will always be in range (assuming ASCII) and 0-9 will always be in range (by C standard). But "special characters" could be anything. Do you include control characters, extended ASCII characters, non-alphabetical extended ASCII characters ?
    Either use the isxxx (isupper, islower, isdigit, isgraph etc) functions in ctype.h, or just build a list of your own, by hand. Then loop over the histogram, checking on the index and summing if positive.
    I'd say always go with a library function if you can. Better to abstract that stuff away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Required Modifying Code
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2011, 12:23 PM
  2. self modifying code
    By chacham15 in forum C Programming
    Replies: 15
    Last Post: 09-07-2008, 04:05 PM
  3. about accessing special symbols
    By godhand in forum C Programming
    Replies: 7
    Last Post: 09-29-2003, 04:40 AM
  4. Self modifying code
    By orbitz in forum Linux Programming
    Replies: 1
    Last Post: 06-06-2003, 06:09 PM
  5. Re : convert character into special code
    By Wah in forum C Programming
    Replies: 0
    Last Post: 01-31-2002, 12:55 AM