By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Need a few pointers in C

P: 1
Hi, after a little help if possible please folks

I am wanting to create a program that accepts an input of characters using gets(), then analyse the string and count the occurance of each letter of the alphabet.

Is it possible to do this in one function, or would I have to repeat it 26 times?

Also, to attain this function, I was going to construct it from an IF statement that is nested in a WHILE loop.

Is the the best way of appraoching this problem, or is there a simpler solution?

Many thanks in advance
Nov 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: 14
Use a for loop from a to z and add up the no of times each alphabet occurs,
u will need a double for loop
Nov 8 '06 #2

10K+
P: 13,264
Use a for loop from a to z and add up the no of times each alphabet occurs,
u will need a double for loop
I suggest you use 2 functions
The first one takes a char array and a char and returns the position of this character in the array. If this is called say getPosition, then


in the other function or in main
Define a char[] called alphabet and initialise it with the 26 characters.
Define an int[] called frequencies of size 26 and initialise it with zeros.
You would use a for loop as suggested but it would not go from a to z but from 0 to the number of characters entered.
at each step of the for loop you do
frequencies[getPosition(alphabet, character)] = frequencies[getPosition(alphabet, character) ] + 1;

where character is the input character
Nov 8 '06 #3

P: 14
try this:

include- iostream, stdio,string.h,
char n[50];
char x='A';
char s='a';
int f=0;
gets(n)
while (x<='Z' && s<='z')
{
for (int y=0;y<=strlen(n);y++)
{
if (x==n[y]||s==n[y])
f++;
}
cout<<"frequency of "<<x<<"is"<<f;
f=0;
x++;
s++;
}
Nov 8 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.