"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, February 18, 2010

Write a program to count the number of words in a string as well as the number of characters other than a space.

#include <stdio.h>
#include <string.h>

void main()
char sen[50];
int word_count = 0, chr = 0, space = 0;

cout<< "\n Enter A String :";


for(int i = 0; sen[i] != '\0' ; i++)
{ if (sen[i] == ' ')
 space++; }

chr = strlen(sen) - space;
 word_count = space + 1;

cout<< "\n The Total no. of words = " << word_count;

cout << "\n The total characters without spaces = " << chr;


1 comment:

  1. it doesn't work if we enter more than 1 space b/w 2 words...


C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.