Hi,

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>
#include<conio.h>
using namespace std;

void countAlpha(char[]);

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

	cout<<"Enter a string: ";
	cin.getline(aa,first);
	

	getch();
	return 0;
}

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

    }
    return;
}

Thank you in advance.

why don't you post it on the C++ section? LOL

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.

Based on my understanding of the problem, you should have a loop (why a loop?) for every letter...

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

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?

I havn't even heard of the other library, but I aggree, cin and cout would do just fine for that purpose

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.

A=0.461
B=0.0769
G=0.0769
H=0.0769
I=0.0769

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]++;
			}
		}

		count_letter_total++;
	}

	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;
}
commented: Do NOT give code away, now the OP has learned nothing. -1
commented: What Jonsca said... -3
commented: Excellent +0

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.

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.