Answer to Question #188871 in C++ for Asad

Question #188871

Write a program to take user input for a string.

  • Count number of spaces in that string.
  • Count number of words in that string.

 

1
Expert's answer
2021-05-08T02:36:14-0400

Answer:-


#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
    char str[100];
    int i;
    int words=1,characters=0,space=0;
    cout<<"Please enter the string \n";
    gets(str);
    for(i=0; str[i] != '\0'; i++){
            if(str[i]!=' '){
                characters++;
            }
             else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ 
                words++;
            }
    }


cout<<"\nTotal characters: "<<characters; 
cout<<"\nSpace: "<<(words-1);  
getch();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS