Question #20178

Write a program to count number of spaces in a string.

Expert's answer

#include <iostream>
using namespace std;

#define n 100

void main(){
char str[n];
int i;
for (i=0; i<n; i++) str[i] = NULL;
cout<<"Enter a string: ";
gets(str);
int c=0; i=0;
while (str[i]!=NULL){
if ((int)str[i] == 32) c++;
i++;
}

cout<<"There are "<<c<<" spaces in the string.\n";
}
LATEST TUTORIALS
APPROVED BY CLIENTS