3 Replies - 3359 Views - Last Post: 23 September 2014 - 04:30 AM Rate Topic: -----

#1 TrancePants   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-March 14

Program to Count Uppercase and Lowercase Characters

Posted 22 September 2014 - 10:44 PM

#include <stdio.h>
#include <string.h>


int main()
{
	char string[100];
	int c= 0, count[26] = {0};

	printf("Enter a string:\t", string);
	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("");

        c++;
    }

	for ( c = 0 ; c < 26 ; c++ )
	{
		if( count[c] != 0 )
		printf("%c : %d\n",c+'a',count[c]);
    }

 return 0;
}



This is the output I recive:
Attached Image

I need some help counting the Uppercase Letters.:)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Program to Count Uppercase and Lowercase Characters

#2 TrancePants   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-March 14

Re: Program to Count Uppercase and Lowercase Characters

Posted 22 September 2014 - 11:40 PM

*******************EDIT***************

#include <stdio.h>
#include <string.h>


int main()
{
	char string[100];
	int c = 0, lower[26] = {0}, upper[26] = {0};

	printf("Enter a string:\t", string);
	gets(string);

	while ( string[c] != '\0' )
    {

		if ( string[c] >= 'a' && string[c] <= 'z' )
            lower[string[c]-'a']++;

		else if(string[c] >= 'A' && string[c] <= 'Z' )
            upper[string[c]-'A']++;

	else
		printf("");

        c++;
    }

	for ( c = 0 ; c < 26 ; c++ )
	{
		if( lower[c] != 0 )
		printf("%c : %d\n",c+'a',lower[c]);
    }
	for ( c = 0 ; c < 26 ; c++ )
	{
		if( upper[c] != 0 )
		printf("%c : %d\n",c+'A',upper[c]);
    }
 return 0;
}




I have created seperate arrays for the lower and uppercase letters and it worked!.
Was This Post Helpful? 1
  • +
  • -

#3 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Program to Count Uppercase and Lowercase Characters

Posted 23 September 2014 - 12:33 AM

Just a few comments.

gets() is considered unsafe you should use fgets(string,[int size],stdin);

<ctype.h> has function like islower() and isupper() which could replace your if conditions.
you could #define your own as well.

This is just personal preference, but I think it makes it easier to assign a array lookup to a variable if is repeated.
So you would do
	while ( string[i] != '\0' )
    {
                char c = string[i];
		if ( islower(c) )
                      lower[c-'a']++;

		else if(isupper(c) )
                      upper[c-'A']++;

	       else
		       printf("");

               i++;
    }


Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Program to Count Uppercase and Lowercase Characters

Posted 23 September 2014 - 04:30 AM

Glad you got it, but you really are trying too hard. If you simply count everything...

Also, you have some warnings and a gets.

Here's another approach:
#include <stdio.h>
#include <string.h>

int main() {
    char string[100];
    int c, counts[256] = {0};

    // error
    // printf("Enter a string:\t", string);
    printf("Enter a string:\t");
    // never, never, never, gets(string); 
    fgets (string, 100, stdin);

    while ( string[c] != '\0' ) { counts[string[c++]]++; }

    for (c = 'a'; c<='z'; c++ ) {
        if( counts[c] != 0 ) { printf("%c : %d\n",c, counts[c]); }
    }
    for (c = 'A'; c<='Z'; c++ ) {
        if( counts[c] != 0 ) { printf("%c : %d\n",c, counts[c]); }
    }
    return 0;
}



Alternately, you could have a single loop and print out the ones you like. However, since you're putting lower before upper, that would be awkward. You could also play with functions here, if you like.

Overall, good work. Happy programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1