Design a program that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet (a to z) found in the text. If a non-alphabetic character is found, simply count how many were found and display that total at the end of the program.

Output should look similar to:

Letter  Count
------    -------
a           6
b         24
…
z           0

There were 5 letters that were non-alphabetic.

I have got the program to put the phrase into the 2d array but I do not know how to be able to count each letter alone. Rite now it is working and is outputting the number of letters and number of chars. If anyone had any idea how to solve this problem please let me know i have the pseudocode already put in i just dont know how to do what i want to do. The only idea I have is to make another array, but even after that im still kind of confused. I think we are suppose to use pointers or ascii or maybe both. Thank you for everyones time to read this and provide any helpfull feedback.

int main( )
{



char textLine[numRows][lineSize] = {0};
char display[26][2] ={0};
int letterCount=0;
int charCount=0;



// prompt for input
cout << "Please insert data"<<endl;


// loop through the sentences
// read the input from the keyboard and convert to lowercase
for(int i=0; i < 3; i++)
{
cin.getline(&textLine[0], 80);
for(int j=0; textLine[j]!= '\0'; j++)
{
// check each letter to see if it is alphabetic
// determine which letter it is and increment letter count by 1
if( isalpha(textLine[j]))
{
letterCount++;
}
else
{
charCount++;
}



}
}
cout<<letterCount<<endl;
cout<<charCount<<endl;



// display a-z tally in a column
// display count of non alpha


return( 0 );
}

Recommended Answers

All 6 Replies

Welcome to DaniWeb. Please learn to use code tags when posting code to this site. See the watermark in the posting window or the announcement section at the top of the board to learn how.

First I think you need to determine whether the char need be case sensitive or not. If not, then it's probably easiest to convert the input into either all caps or all lower case. Then you could use a switch statement with the char as the parameter sent to the switch or, frequently used to do this, an array of ints with the index to represent the distance from 'A' or 'a'.

Welcome to DaniWeb. Please learn to use code tags when posting code to this site. See the watermark in the posting window or the announcement section at the top of the board to learn how.

First I think you need to determine whether the char need be case sensitive or not. If not, then it's probably easiest to convert the input into either all caps or all lower case. Then you could use a switch statement with the char as the parameter sent to the switch or, frequently used to do this, an array of ints with the index to represent the distance from 'A' or 'a'.

Yes i do need to convert the input to all lowercase. And i am not suppose to use a switch statement which would very long this program is suppose to be very short i just can't figure out how to convert all the input lowercase then check all the letters and chars and to increment their counters i am sure i need more arrays or more variables i just dont know exactly what i'm jus stumped

As for the lowercasing, I have a function I created (with help from someone else) that could help you. First, you should try it for yourself though. If you can't I can give you the code. Try using a for loop that starts at 0 and goes to the length of the input. Use an if statement to check if it is a capital letter, and if it is, make it lowercase. Capital 'A' is equal to 0x41 in hexcode and capital 'Z' is equal to 0x5A in hex code. Use those for the if statement, and use 'A' - 'a' to tell how to change a capital letter to a lowercase letter.

As for the lowercasing, I have a function I created (with help from someone else) that could help you. First, you should try it for yourself though. If you can't I can give you the code. Try using a for loop that starts at 0 and goes to the length of the input. Use an if statement to check if it is a capital letter, and if it is, make it lowercase. Capital 'A' is equal to 0x41 in hexcode and capital 'Z' is equal to 0x5A in hex code. Use those for the if statement, and use 'A' - 'a' to tell how to change a capital letter to a lowercase letter.

thanks i will try this and see what i can do this is my first year in programming so it is kinda hard

As for the lowercasing, I have a function I created (with help from someone else) that could help you. First, you should try it for yourself though. If you can't I can give you the code. Try using a for loop that starts at 0 and goes to the length of the input. Use an if statement to check if it is a capital letter, and if it is, make it lowercase. Capital 'A' is equal to 0x41 in hexcode and capital 'Z' is equal to 0x5A in hex code. Use those for the if statement, and use 'A' - 'a' to tell how to change a capital letter to a lowercase letter.

hey if you could show me how to do this that would awesome i have been trying for the past day and i can't get anything to work, so if you get time if you could post the function that would be awesome thank you so much

There is a standard function called tolower() so you don't have to do this yourself. If you are doing so as a learning experience then find yourself an ASCII table and look at the relationship of the 'A' to the 'a', the 'B' from the 'b', etc, in terms of the integer equivalent of each. You can find copies of the table by doing a web search if it isn't in one of the reference books on your shelf.

To count # of each unique char without using switch then I'd use an array where the array element with index zero was equivalent to 'a' and the element with index 1 was the equivalent of 'b'. As discussed above, each char is stored in the computer as an integer value, the exact value of which is determined by which char table you use, ASCII is very commonly used in implementations of C/C++. However the distance from 'a' can be determined by subtracting the value of 'a' from another given char. So 'a' - 'a' == 0, 'b' - 'a' == 1, etc. That distance can then be used as the index of an array element to access a given counter for the char from which 'a' is subtracted. So if the distance was 14 then increment the value at array[14] by 1, etc.

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.