Write a program to take user input for a string.
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;
}
Comments
Leave a comment