C++ Complete the body of this function. Use a queue of characters to store the i
ID: 3773214 • Letter: C
Question
C++
Complete the body of this function. Use a queue of characters to store the input line as it is being read.
size_t counter( )
// Precondition: There is a line of input waiting to be read from cin.
// Postcondition: A line of input has been read from cin, up to but not
// including the newline character. The return value of the function
// is the number of times that the LAST character of the line appeared
// somewhere in this line.
// EXAMPLE Input: ABBXDXXZX
// The value returned by counter would be 4 for this input since there
// are 4 X's in the input line.
{
size_t answer = 0;
queue q;
Explanation / Answer
size_t counter( )
// Precondition: There is a line of input waiting to be read from cin.
// Postcondition: A line of input has been read from cin, up to but not
// including the newline character. The return value of the function
// is the number of times that the LAST character of the line appeared
// somewhere in this line.
// EXAMPLE Input: ABBXDXXZX
// The value returned by counter would be 4 for this input since there
// are 4 X's in the input line.
{
//variables declaration
size_t answer = 0;
queue q;
//reading string from user
string word;
cout<<"Enter your String: ";
cin>>word;
//calcualting length
int length = word.length() + sizeof('');
//pushing each character into queue
for(int i=0;i<length;i++){
q.push(word[i]);
}
//comapring with last ahcaracter in total string
int totalCharCount = 0;
while (!q.empty())
{
//pulling each character
if(q.front() == word[length-1]){
totalCharCount++;
}
//popping out
q.pop();
}
//displayig total
cout<<" Total chars found in word is: "<<totalCharCount;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.