Prompt the user to enter a sentence read in AT MOST 80 chara

Prompt the user to enter a sentence read in AT MOST 80 characters of the sentence the user entered count the number of words in the sentence by looking for whitespace (space, tab) 4) print out the number of words in the sentence (that was at most 80 characters The idea: User inputs: A long sentence that will get us started on the lab so that we can see what is really going on here Your code should only read in A long sentence that will get us started on the lab so that we can see what is r And count the number of words and report the results: The 80 characters read were: A long sentence that will get us started on the lab so that we can see what is r and they contained 19 words

Solution

#include <iostream>

using namespace std;

void word_count(char str[]); // function prototype

int main()
{
char s[100]; // keeping the string length more than 80 for robustness
int len; // temporary variable to check the input string length
cout<<\"Enter any string of more than 80 characters:\";
  
cin.getline(s,81); // consider only first 80 characters (+1 for null character) from input string
  
   for(len=0; s[len]!=\'\\0\';len++); // Loop through the string to make sure that only 80 characters are
                               // taken in consideration.
cout<<\"The length of the string is : \"<<len<<endl ; // should input <=80 (if input is less than 80 character, then
                                                       // all the characters are copied to s)
  
word_count(s); // call the word_count function to calculate the number of words in the 80 chars string
return 0;
}

void word_count(char str[])
{
int words=0;
for(int i=0;str[i]!=\'\\0\';i++)
{
if (str[i]==\' \' || str[i]==\' \') // check for spaces or Tabs
words++; // store the word count value
}
cout<<\"The number of words=\"<<words+1<<endl;
}

 Prompt the user to enter a sentence read in AT MOST 80 characters of the sentence the user entered count the number of words in the sentence by looking for whi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site