#include <stdio.h>
#include<ctype.h>
main() {
	int c,nletters[26],i;	
	for (i = 0; i < 26; ++i)
		nletters[i] = 0;
	while ((c = getchar()) !='!') {
		if(isupper(c))
		c=tolower(c);
		++nletters[c - 'a'];			
	}
	for (i = 0; i < 26; ++i) {
		c = 'a' + i;
                  if(nletters[i] != 0){
		putchar(c);
		printf(" = %d\n", nletters[i]);}
	}
}

Recommended Answers

All 8 Replies

Nah, how about you ask a specific question about the part you don't understand? What do you think it does?

Maybe you should try running the code and see what happens

The above question is as follows.

Write a program to count the occurrence of letters in the range a - z and A - Z. Ignore other chars.

Output for input: aAbcBc

a = 2
b = 2
c = 2

Why is the array not declared ?

The program accepts alphabets and maintains the count of occurance of each alphabet in one array (for all 26 letters).
But the range of alphabets considered here does not differentiate between lowercase [a-z] and uppercase [A-Z] and thus converts the uppercase entries also to lowercase to make the calculation simple.

isupper function checks for uppercase characters and tolower function converts it to lowercase.

Anyway your input should be aAbcBc! because the entry has to terminate with a "!" character. And the output you got tells the rest.

N.B: To meet your program requirements (as per your WAP statement) try to distinguish between lowercase and uppercase characters.

Hope you got it...

please note some small syntax changes i have made to your code. although they seem like a burden, this sort of correctness and neatness will make the code clearer and easier to understand. best of luck! ;)

#include <stdio.h>
#include <ctype.h>
int main() {
int c, nletters[26], i;	
	for (i = 0; i < 26; ++i)
		nletters[i] = 0;
	while ( (c = getchar()) != '!' ) {
		if (isupper(c))
		c=tolower(c);
		++nletters[c - 'a'];			
	}
	for (i = 0; i < 26; ++i) {
		c = 'a' + i;
                if(nletters[i] != 0) {
			putchar(c);
			printf(" = %d\n", nletters[i]);
		}
	}
return 0;
}

Thank You

hai anil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.