Saturday, January 30, 2016

C program to count words in a given string.

#include<stdio.h>
#include<stdlib.h>

int main(){

char str1[80]="Hello Student How are you?", *token ;
const char s[2] = " ";
    
int counter=0;
printf("Given String = %s",str1);
token = strtok(str1,s);
while (token != NULL)
{
counter++;
token = strtok(NULL, s);
}
printf("\nTotal words in a given string is %d", counter );
 return 0;
}

Output of program:
Given String = Hello Student How are you?
Total words in a given string is 5

No comments:

Post a Comment