I need help with this C++ programming problem. Please note, this is C++, not Jav
ID: 3669733 • Letter: I
Question
I need help with this C++ programming problem. Please note, this is C++, not Java language Write a program scrabble_game.cpp that reads in text one character at a time and counts the number of occurrences of the following letters. 'e', "d, 'c, 'h, k’, and "j ’ in the text, and determines a total score for the text using a point based scheme. Both lower case and upper case letters should be counted together. When a period, , or exclamation mark, , appears in the input textthe program prints how many of each letter above only, the total score of the text (see point assignments below), and halts. Each letter being counted is assigned a score (similar to the board game Scrabble) as follows: 'e' (1 point, 'd' (2 points), 'c' (3 points), 'h' (4 points), K' (5 points), and 'j (8 points)All other characters have a score of 0 points. You will compute the total score of the input text by summing the number of occurrences of a letter above multiplied by its score. For example, if the input text is: she worked hard to build the deck Just right., then the counts are 4e, 4 “d, 1 c', 4 'h ’, 2 k', and 1 j. The total score for the phrase is 4 x 1 + 4 2 + 1 x 3 + 3 x 4 + 2 x 5 + 1 x 8 = 49 Here are the steps to help you code this problem. Program scrabble_game .cpp . a. To read in a single character, use cin to read a value of type char. b. Use a while loop to read and count the characters in the input. Read in a character, check that the input character is not the period, P, nor the exclamation mark, I 3, and then enter the while loop. C. Be sure to read in the next character at the end of the while loop. d. Lower case and upper case vowels should be counted together. Thus a text with three characters "e® and four characters 'E' will have a total of 7 occurrences of e e. You can compute the total score using the point values of each letter we are counting as you count each letter in the while loop or after you have finished determining the counts, ie . after the while loop. f. Run your program on various input including boundary cases such as just a period, ", or just an exclamation mark, ! , as input.Explanation / Answer
Program Plan: As the user asks i am taking character array to store the string and reads one character at a time by using while loop.then compares that character with the given characters.Also we need to maintain the 6 different variables to count 6 characters.and i am using strlwr function which is used to convert the entire string into the lower case.
Program:
//including the essential library functions
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char n[100]; //character array which stores the given string.
int i,ec,dc,cc,hc,kc,jc,score; //score is the variable store the calculated score
//i is the loop variable,ec=e's count variable;dc=d's count variable similarly cc,hc,kc,jc are the count variables
//to count the noof occurances
clrscr();
cout<<" Enter input text: "; //Reading the input
cin>>n;
strlwr(n); //strlwr function will convert the entire string to lower case
i=0;ec=0;dc=0;cc=0;hc=0;kc=0;jc=0; //initialising all count variables to zero
while(n[i]!='!'||n[i]!='.') //loop will exit whenever it reaches to '.' an '!'
{
//now we use if else loop for the character count
if(n[i]=='e')ec++;
else if(n[i]=='d')dc++;
else if(n[i]=='c')cc++;
else if(n[i]=='h')hc++;
else if(n[i]=='k')kc++;
else if(n[i]=='j') jc++;
i++; //incrementing the loop variable
}
//the loop will run until it reaches dot or exclametry mark.
score=(ec*1)+(dc*2)+(cc*3)+(hc*4)+(kc*5)+(jc*8);
//Displaying all variables
cout<<"number of occurances of e's(worth 1 point each) "<<ec;
cout<<" number of occurances of d's(worth 2 point each) "<<dc;
cout<<" number of occurances of c's(worth 3 point each) "<<cc;
cout<<" number of occurances of h's(worth 4 point each) "<<hc;
cout<<" number of occurances of k's(worth 5 point each) "<<kc;
cout<<" number of occurances of j's(worth 8 point each) "<<jc;
cout<<" Total score is"<<score;
getch();
}
Sample Output:
enter text: carroms is piece of cake for me.
number of occurances of e;(worth 1 point) :4
number of occurances of d;(worth 1 point) :0
number of occurances of c;(worth 1 point) :2
number of occurances of h;(worth 1 point) :0
number of occurances of k;(worth 1 point) :1
number of occurances of j;(worth 1 point) :0
Total score:15