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
as example
words("my name is mohamed") will return 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
No comments:
Post a Comment