I am newbie to c++.
I would like to en quire on how to calculate the number of same alphabet in a string that we enter?

For example if let say i have entered ABAAGAYHAUIJA
and it should return 6.

Here's my code.

#include <iostream>
using namespace std;

void countAlpha(char[]);

int main()
	const int first=1000;
	char aa[first];

	cout<<"Enter a string: ";

	return 0;

void countAlpha(char len[])
    int count = 0, m = 0;
    while (len[m] != '\0')
          if (len[m] == 'A')


Thank you in advance.

A few questions:
Are you only going to count the number of instances of the first character encountered in your input (the string), or count how often each different character occurs?
Are you supposed to use arrays?

Two main problem points:
1/ in main() you never call your method 'void countAlpha(char len[])'

2/ there's no output - when should it show the answer?

There's more - but that's enough for the moment.

for the
int getch(void); (amongst others)

Now, I bet you know why #include <iostream> is in there!

Seriously, that's the sort of thing you could look up yourself

what's in the header file that's called "conio.h"?

for the
int getch(void); (amongst others)

The real question is why is the O/P using a non-standard C function from conio.h in a C++ program when a simple cin.get() will suffice?

@Walt - a valid point; it was something we could have got at after we knew exactly what was wanted here
That's essentially what I was trying to get out of the OP - ie in what direction does (s)he wants to go?

Edited 5 Years Ago by Dingbats: Spelling - again!

I need to count the number of each alphabet and the proportions of it. Sorry for not to mention on "For example if let say i have entered ABAAGAYHAUIJA and it should return A=6, B=1, C=0, D=0, E=0, F=0, G=1, H=1, I=1, J=1, K=0, L=0, M=0, N=0, O=0, P=0, Q=0, R=0, S=0, T=0, U=0, V=0, W=0,Y=0, Z=0".
Next I should able to show on the proportions of each alphabet.


Can anyone please help me on this?

Use an array of ints that's 26 elements long. Use arithmetic to transform the letter you are reading into an index of that array (where 'A' corresponds to element 0 of the array).

Is this what you want:

#include <iostream>
using namespace std;

double percentage (int count_letter, int count)
	double percentage, calc_a (count_letter), calc_b (count);

	percentage = calc_a / calc_b;
	percentage = percentage * 100;

	return percentage;

int main ()
	char text [2048], alphabet;
	int count_letter_total (0), count_letter_individual [26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	cout << "Frequency Analysis\n";
	cout << "==================\n";
	cout << "\n";
	cout << "Please enter text below:\n";
	cin >> text;
	cout << "\n";

	while (count_letter_total < 2048 && ((text [count_letter_total] >= 65 && text [count_letter_total] <= 90) || (text [count_letter_total] >= 97 && text [count_letter_total] <= 122)))
		for (int i = 65; i <= 90; i++)
			if (text [count_letter_total] == i || text [count_letter_total] == i + 32)
				count_letter_individual [i - 65]++;


	for (int i = 0; i <= 25; i++)
		alphabet = i + 65;
		cout << alphabet << ":\t" << count_letter_individual [i] << "\t" << percentage (count_letter_individual [i], count_letter_total) << "%\n";

	cout << "\nPress any key to exit...";
	system ("pause>nul");

	return 0;

Edited 5 Years Ago by SoulReaper1680: n/a

What Jonsca said...
Do NOT give code away, now the OP has learned nothing.

SoulReaper1680: First of all I would like to apologize for the late reply. Anyway thank you so much for the help, to solve my problem.

This article has been dead for over six months. Start a new discussion instead.