i'm a c++ beginner and now i got a question
i try many times but i stil can't solve it
so can anyboby here help me to solve?

here is my question:

Write a program that read in a sequence of characters in terms of sentences. Provide analysis of the input characters such as the number of characters of a, b and so on. Assume all characters are in lower case. Use a class concept in your program.

Sample outputs:

Enter your sentence: a program consists of a number of instructions separated by semicolon.

Analysis:
Character a: 5
Character b: 2
…..
character z: 0


this is my code:

#include<iostream>
using namespace std;

class analysis {
	char sentence[80];
	char str[80];
	int counter;
	int count;

public:
	void read();
	void compare();
	void display();
	void character();
};

void analysis::read()
{
	cout << " Enter your sentence: " ;
	cin.getline(sentence,80);

}

void analysis::character()
{
	str={'a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
}

void analysis::compare()
{
	for(int i=0; sentence[i]; i++)
	{
		for(int j=0; sentence[j]; j++)
		{
			if(strcmp(str,sentence)==0)
			count+=counter[j];
		}
	}
}

void analysis::display()
{
	cout << " ANALYSIS : " << endl;

	for(int m=0; m<strlen(sentence); m++)
		cout << " Character " << str[m] << " : " << counter[m] << endl;

}

void main()
{
	analysis word;

	word.read();
	word.character();
	word.compare();
	word.display();
}

if i want to try to use the ASCII code, how can i use it?

first you need to declare an array of integers that represent the count for each characters -- since there are 26 letters in the English alphabet ('a' - 'z]) you will need an array of 26 integers.

int counters[26] = {0};

The abover declared the array and initialized them all to 0.
Next step, after entering the sentence, increment the counters array for each character in the sentence. Loop through the sentence and increment the array element that corresponds to the character. To get the index value, subtract the letter 'a' from the character. Example: if sentence == 'a', then 'a' - 'a' == 0. You could look up the ascii value for 'a' and use that, but its a lot easier just to use 'a'.

char sentence[80];
for(int i = 0; sentence[i] != 0; i++)
   counter[sentence[i]-'a']++;

you should be able to finish the rest of the program yourself.

now i understand more
thanks a lot!

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.