-->
Showing posts with label 7)WAP to count total words in a string.. Show all posts
Showing posts with label 7)WAP to count total words in a string.. Show all posts

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