Number of words in a string

Keane 16

Skilled
//Count number of words.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main()

{

clrscr();

char s[100];

int len,i,space,letter;

gets(s);

len=strlen(s);

for(i=0; i<=len; i++)

{

if(s==0)

space++;

else

letter++;

}

cout<<"Number of words= "<<space+1<<endl;

cout<<"Number of letters = "<<letter<<endl;

getch();

}


What's wrong with the prog ???? :S
 
Code:
//Count number of words.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

void main()

{

clrscr();

char s[100];

int len,i,space=0,letter=0;

gets(s);

len=strlen(s)-1;

for(i=0; i<=len; i++)

{

if(s[i]==' ')

space++;

else

letter++;

}

cout<<"Number of words= "<<space+1<<endl;

cout<<"Number of letters = "<<letter<<endl;

getch();

}

and thank my sister for this :bleh:

also , you forgot to initialize the vars... always....ALWAYS initialize... also, your loop seems flawed.. thats why the

Code:
len=strlen(s)-1

If it were me , i would have done a do while to check for s! = '/0'
 
Back
Top