AD1

Saturday, 11 February 2017

Number of words in a string

Write a C function words(string) that accepts a string as input parameter and returns the number of words in this string 
as example
words("my name is mohamed") will return 4


#include <stdio.h>
#include <string.h>
int words(char *s)
{
int i=0;
int counter=0;
if(strlen(s)==0){return 0;}
else{
while(*(s+i)!='\0')
{
if(*(s+i)==' '){counter++;i++;}
i++;
}
return counter+1;
}
}
int main(int argc, char **argv)
{
char statement[]="";
printf("number of words are\n%d\n",words(statement));
return 0;
}
view raw no_of_words.c hosted with ❤ by GitHub

No comments:

Post a Comment