Tuesday, May 24, 2016

NIELIT M3-R4: Q.No. 5(a) January 2014

 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();
}

No comments:

Post a Comment