| | |
Read a phrase and Display the amount of letters.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
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[i][0], 80);
for(int j=0; textLine[i][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[i][j]))
{
letterCount++;
}
else
{
charCount++;
}
}
}
cout<<letterCount<<endl;
cout<<charCount<<endl;
// display a-z tally in a column
// display count of non alpha
return( 0 );
}
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[i][0], 80);
for(int j=0; textLine[i][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[i][j]))
{
letterCount++;
}
else
{
charCount++;
}
}
}
cout<<letterCount<<endl;
cout<<charCount<<endl;
// display a-z tally in a column
// display count of non alpha
return( 0 );
}
•
•
Join Date: Jul 2005
Posts: 1,704
Reputation:
Solved Threads: 274
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'.
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'.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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'.
•
•
Join Date: Feb 2008
Posts: 67
Reputation:
Solved Threads: 1
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.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jul 2005
Posts: 1,704
Reputation:
Solved Threads: 274
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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: tree class and list class
- Next Thread: Passing values between functions
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






