Counting occurances of numbers and letters

Hello everyone. I'm trying to create a program with 2 functions that will take the characters within a string and output the occurances of letters and numbers within that sentence. This is what I have so far:

My header file:

1
2
3
4
5
6
7
8
9
class Count
{
        char buffer[150];      //Maximum characters allowed in a sentence
        char asciibuffer[122]; //Buffer for ascii characters
	short numberofcharacters;
public:
	void DataInput();
	void CharacterCount();
}


My 2 functions in question: (Main is a switch statement menu that calls these 2 functions)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void Count::DataInput()
{
	char sentence;
	
	cout << "Enter a sentence: \n";
	cin.getline(sentence,sizeof(sentence));
	numberofchacters = cin.gcount();
	return numberofchacters;
}

void Count::CharacterCount()
{
	int i;
	int lowercase, uppercase, numbers, sentence[i];

	while(sentence[i] < numberofchacters)
	{
		if(sentence[i] >= 'a' && sentence[i] <= 'z')
			lowercase++;
		if(sentence[i] >= 'A' && sentence[i] <= 'Z')
			uppercase++;
		if(sentence[i] >= '0' && sentence[i] <= '9')
			numbers++;
		i++;
	}
	cout << sentence[i][lowercase] << sentence[i][uppercase] << sentence[i][numbers];

}


I'm trying to use a 2D array here for the output. I have the ascii char set in front of me (A-Z = 65-90, a-z = 97-122, 0-9 = 48-57) but i'm lost as to how to typecast the ascii buffer within the array in order to show the letter/numbers with the corresponding occurances.
If you just want to know how many lowercase/uppercase/numbers this should suffice:

cout << "lowercase: " << lowercase << ", uppercase: " << uppercase << ", numbers: " << numbers;


but i'm lost as to how to typecast the ascii buffer within the array in order to show the letter/numbers with the corresponding occurances.
This seems something else. Do you want to know how many times a letter/number occurs in the sentence?
This seems something else. Do you want to know how many times a letter/number occurs in the sentence?


Yes, that's what i'm trying to output. Something similiar to this:

1
2
3
4
Output:
a - 4
b - 3
0 - 2
std::map or use your own hash table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::map<char,int>count;
	std::map<char,int>::iterator itr;
	char array[]={'a','b','a','c','b','g'};
	unsigned i=0;
	while(array[i]!='\0')
    {
    count[array[i]]++;	
    i++;

    }	
    for(itr=count.begin();itr!=count.end();itr++)
    {
    	std::cout<<itr->first<<" "<<itr->second<<"\n";
    }
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Count
{
        char buffer[150];      //Maximum characters allowed in a sentence
        char asciibuffer[122]; //Buffer for ascii characters
	short numberofcharacters;
public:
	void DataInput();
	void CharacterCount();
};

void Count::DataInput()
{
	cout << "Enter a sentence: \n";
	cin.getline(buffer,sizeof(buffer) - 1);
	numberofchacters = strlen(buffer);
}

void Count::CharacterCount()
{
        int asciibuffer[122] = {0}; //Buffer for ascii characters
	
	for(int i = 0; i < numberofchacters; ++i)
	{
		if(buffer[i] >= 'a' && buffer[i] <= 'z')
			asciibuffer[buffer[i]]++;
		if(buffer[i] >= 'A' && buffer[i] <= 'Z')
			asciibuffer[buffer[i]]++;
		if(buffer[i] >= '0' && buffer[i] <= '9')
			asciibuffer[buffer[i]]++;
	}
for(int i = 0; i < 122; ++i)
{
	if(asciibuffer[i] > 0)
		cout << char(i) << ": " << asciibuffer[i];
}
}
Topic archived. No new replies allowed.