Count Uppercase and Lowercase Characters

This is a discussion on Count Uppercase and Lowercase Characters within the C Programming forums, part of the General Programming Boards category; I have been given this assignment: Write a complete program using two arrays, upper and lower to keep the upper ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    19

    Count Uppercase and Lowercase Characters

    I have been given this assignment:

    Write a complete program using two arrays, upper and lower to keep the upper
    And lower alphabet respectively.
    Ask the user to enter string example:
    This is a test from Jupiter. Soon you will see who is from Jupiter!!! May be Dr. D.
    Your program should parse the string and keep track of number of alphabet. Both arrays are indexed from 0 to 25. The logical way to do this is to use upper[0] to
    Count the number of A, and upper[1] to count number of B and so on. Likewise
    For the lower array.

    Output should look like:
    A: 0 a:2
    B: 0 b:0

    This is what I have...

    insert
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
     char string[100];
     int c = 0, count[26] = {0};
    
    printf("Enter a string:\t");
     gets(string);
    
     while ( string[c] != '\0' )
        {
    
     if ( string[c] >= 'a' && string[c] <= 'z' )
                count[string[c]-'a']++;
    
     else if(string[c] >= 'A' && string[c] <= 'Z' )
                count[string[c]-'A']++;
    
     else
     printf("ERROR!!\n");
    
            c++;
        }
    
     for ( c = 0 ; c < 26 ; c++ )
        {
     if( count[c] != 0 )
     printf("%c : %d\n",c+'a',count[c]);
        }
    
     return 0;
    }
    
    


    However, it does not differentiate between uppercase and lowercase letters, even though it must. Am I to create a separate variable for the uppercase? If yes, how do I implement it? If no, what should I do? Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Yes, and in fact your instructions explicitly state that you should have separate arrays for uppercase and lowercase letters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    I assumed so but I am unsure on how to implement the uppercase array. Am I supposed to create an entirely separate loop for it? Thank you.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Not necessarily. Notice that in your current code (which is badly in need of proper formatting), you are updating the count array in two different places. You could change one of these places without changing the other.

    By the way, although subtraction will almost certainly work for you, there is no requirement that the letters of the alphabet will be contiguous in the character set.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    All right, thank you very much. I will edit the code and reply if there are any problems. I appreciate all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 07:16 AM
  2. how to detect a lowercase or uppercase char?
    By Axel in forum C Programming
    Replies: 5
    Last Post: 09-04-2005, 12:28 PM
  3. Uppercase and lowercase
    By StarOrbs in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2005, 03:18 PM
  4. Uppercase & Lowercase
    By Dennis in forum C Programming
    Replies: 2
    Last Post: 11-14-2002, 07:07 AM
  5. converting chars to uppercase/lowercase
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2002, 07:55 PM

Tags for this Thread


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