-->

WAP to count total words in a string.

in turboc++
-------------------------------------------------------------
//program to count total words in a string.
#include <stdio.h>
#include<conio.h>
int main()
{
    char string[100],sec;
    int i,count=1;       //initialization
    printf("enter string\n");
    gets(string);//accepts string
    for(i=0;string[i]!='\0';i++)//loop execution till null character
    {
        if(string[i]==' ')//condition testing for character
        {
            count++;//counting  words
        }
    }
    printf("total words=%d",count);// prints total words.
    getch();
    return 0;
}
---------------------------------------------------------------------------------------------------------
in codeblocks:
---------------------------------------------------------------------------------------------------------------
//program to count total words in a string.
#include <stdio.h>
int main()
{
    char string[100],sec;
    int i,count=1;       //initialization
    printf("enter string\n");
    gets(string);//accepts string
    for(i=0;string[i]!='\0';i++)//loop execution till null character
    {
        if(string[i]==' ')//condition testing for character
        {
            count++;//counting  words
        }
    }
    printf("total words=%d",count);// prints total words.
    return 0;
}

No comments:

Post a Comment