M3-R4: January 2014 (O-Level)
Q. 5(a):
Write a ‘C’ program to count number of words in a string.
Ans:
Program to count number of words in a string:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char str[80];
int i, len, count=0;
printf("\nEnter a string: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
if(str[i] == ' ')
count++;
}
count++;
printf("\nNumber of words in the string is %d", count);
getch();
}
Q. 5(a):
Write a ‘C’ program to count number of words in a string.
Ans:
Program to count number of words in a string:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char str[80];
int i, len, count=0;
printf("\nEnter a string: ");
gets(str);
len = strlen(str);
for(i=0; i<len; i++){
if(str[i] == ' ')
count++;
}
count++;
printf("\nNumber of words in the string is %d", count);
getch();
}
No comments:
Post a Comment