Thread: Counting numbers of upper and lower case letters in C programming?

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    56

    Counting numbers of upper and lower case letters in C programming?

    Hi,
    I was working on a program which count #'s of upper and lower case letters, digits and # of all special characters in a sentence a user enters. So, here is what I got so far:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>

    int main()
    {
    char str[30];
    int len;
    int i;

    printf("\nEnter a sentence:");
    gets(str);
    len = strlen(str);

    for (i=0; i<len;i++){
    if (isalpha(str[i])){
    printf("\nstr[%d] is alpha", i);
    if (islower(str[i]))
    printf("\nstr[%d] is lowecase",i);
    else
    printf("\nstr[%d] is upper", i);
    }
    if (isdigit(str[i])){
    printf("\nstr[%d] is Digit", i);
    }
    else
    printf("\nstr [%d] is Not alpha nor digit");
    }
    return 0;
    }

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    1) Use code tags when posting.
    2) Never use gets().
    3) What is your question? Are you unsure how to create variables for keeping track of the counts?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    56

    Question

    My question is, how to count and display the number of lower and upper letters entered by the user (gets(str)). I tried this code but its not working. I am guessing i am missing something.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Keep a counter of upper and lower case letters...
    Code:
    int cnt_upper = 0, cnt_lower = 0;
    Then inside the if block where you determined whether something is upper / lower, just add 1 to the counter.

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    56
    I tried that and when you run the program it start a endless loop. Can you explain lil more clearly?
    Also, just to make sure that I am writing this program in C language.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Please post your updated code. Intent properly and use code tags.

  7. #7
    Banned
    Join Date
    Apr 2011
    Posts
    56
    There we go again ...
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main()
    {
    	char str[80];
    	int countUpper = 0;
    	int countLower = 0;
    	int len;
    	int i;
    
    	printf("\nEnter a sentence:");
    	gets(str);
    	len = strlen(str);
    	
    	for (i=0; i<len;i++){
    	if (isalpha(str[i])){
    	printf("\nstr[%d] is alpha", i);
    	if (islower(str[i])){
    	printf("\nstr[%d] is lowecase",i);
    	i = countLower + 1;
    	}
    	else 
    	printf("\nstr[%d] is upper", i);
    	i = countUpper + 1;
    	}
    	if (isdigit(str[i])){
    	printf("\nstr[%d] is Digit", i);
    	}
    	else
    	printf("\nstr [%d] is Not alpha nor digit");
    	}
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your indentation is still off. Get an editor that auto-indents and does syntax highlighting, and errors like this will jump out at you. You're missing some braces:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main()
    {
        char str[80];
        int countUpper = 0;
        int countLower = 0;
        int len;
        int i;
    
        printf("\nEnter a sentence:");
        gets(str);
        len = strlen(str);
    
        for (i=0; i<len;i++){
            if (isalpha(str[i])){
                printf("\nstr[%d] is alpha", i);
                if (islower(str[i])){
                    printf("\nstr[%d] is lowecase",i);
                    i = countLower + 1;
                }
                else {
                    printf("\nstr[%d] is upper", i);
                    i = countUpper + 1;
                }
            }
            if (isdigit(str[i])){
                printf("\nstr[%d] is Digit", i);
            }
            else
                printf("\nstr [%d] is Not alpha nor digit");
        }
        return 0;
    }

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Check your indentation - that code is hard to read.

    Increment countUpper by 1 with the statement, countUpper++;, which literally translates to countUpper = countUpper +1;. You're already using the i variable to keep track of the position in the string. Also, use fgets(str, sizeof(str), stdin); to fetch a string from the user, instead of gets.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    	i = countLower + 1;
    ...
    	i = countUpper + 1;
    Also, those lines are wrong. You're changing the value of i instead of countLower and countUpper. Replace them with
    Code:
    countLower++;
    countUpper++;
    Code:
    	printf("\nstr [%d] is Not alpha nor digit");
    You need another parameter to printf. Pass in i.

  11. #11
    Banned
    Join Date
    Apr 2011
    Posts
    56
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main()
    {
    	char str[80];
    	int countUpper = 0;
    	int countLower = 0;
    	int len;
    	int i;
    
    	printf("\nEnter a sentence:");
    	fgets(str, sizeof(str), stdin); 
    	len = strlen(str);
    	
    	for (i=0; i<len;i++){
    		if (isalpha(str[i])){
    			printf("\nstr[%d] is alpha", i);
    			if (islower(str[i])){
    				printf("\nstr[%d] is lowecase",i);
    				countLower ++;
    			}
    			else{ 
    				printf("\nstr[%d] is upper", i);
    				countUpper ++;
    				}
    		if (isdigit(str[i])){
    			printf("\nstr[%d] is Digit", i);
    		}
    		else
    			printf("\nstr [%d] is Not alpha nor digit", i);
    		}
    		}
    	return 0;
    	}
    Still doesnt work .. Here is the output>

    Enter a sentence:Hello World001!

    str[0] is alpha
    str[0] is upper
    str [0] is Not alpha nor digit
    str[1] is alpha
    str[1] is lowecase
    str [1] is Not alpha nor digit
    str[2] is alpha
    str[2] is lowecase
    str [2] is Not alpha nor digit
    str[3] is alpha
    str[3] is lowecase
    str [3] is Not alpha nor digit
    str[4] is alpha
    str[4] is lowecase
    str [4] is Not alpha nor digit
    str[6] is alpha
    str[6] is upper
    str [6] is Not alpha nor digit
    str[7] is alpha
    str[7] is lowecase
    str [7] is Not alpha nor digit
    str[8] is alpha
    str[8] is lowecase
    str [8] is Not alpha nor digit
    str[9] is alpha
    str[9] is lowecase
    str [9] is Not alpha nor digit
    str[10] is alpha
    str[10] is lowecase
    str [10] is Not alpha nor digitPress any key to continue . . .

    Looks Funny.
    Last edited by Iron Hide; 04-11-2011 at 05:54 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please don't just post code without any question or explanation. I can only assume that it's still not working correctly. How about you fix your indentation and see if you can spot anything? Also, for the third and final time, stop using gets. There's a great explanation of why it's bad and how to fix it here.

    If you can manage to fix your indentation and usage of gets, and still have trouble locating your problem, then post your updated (no gets and correct indentation) code, and I will help you. But if you don't listen to me, why should I listen to you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-07-2011, 02:15 PM
  2. Simple program problem from upper to lower case
    By steals10304 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2009, 02:31 AM
  3. help...Lower and Upper case problem only
    By j4k50n in forum C Programming
    Replies: 9
    Last Post: 04-19-2007, 12:39 AM
  4. Remove upper case letters from a char string
    By lavinpj1 in forum C Programming
    Replies: 8
    Last Post: 05-01-2006, 12:09 PM
  5. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM