#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Need help with my code


    Hi, I'm coding a program which count the number of words in the string.
    Example: asfasf[space][space]asfkuef[space]a[space]
    Output:3 words, 14 letters, 4 space

    But I'm getting error with the code. someone help me out?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    	char str[50];
    	int words = 0, len = 0, i;
    	clrscr();
    	printf("\n--------------------------------------------------------");
    	printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS IN A GIVEN STRING");
    	printf("\n\n--------------------------------------------------------");
    	printf("\n\n\t ENTER A STRING...: ");
    	gets(str);
    	while(str[len]!='\0')
    		len++;
    	len--;
    	for(i=0;i<=len;i++)
    	{
    		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
    			words++;
    	}
    	printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
    	printf("\n\n-------------------------------------------------------");
    	getch();
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Hey guy is ok. i solve it by changing void to int and remove clrscr();.

    btw how can i count the number of space? :o
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    93
    Rep Power
    26
    Check out the strtok function. With a little creativity, you should be able to simplify your program significantly.

    http://www.cplusplus.com/reference/cstring/strtok/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    	char str[50];
    	int words = 0, len = 0, i, number;
    //	clrscr();
    	printf("\n--------------------------------------------------------------------------");
    	printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS, STRING, SPACE IN A GIVEN STRING");
    	printf("\n\n--------------------------------------------------------------------------");
    	printf("\n\n\t ENTER A STRING...: ");
    	gets(str);
    	number = strlen(str);
    	while(str[len]!='\0')
    		len++;
    //	len--;
    	for(i=0;i<=len;i++)
    	{
    		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
    			words++;	
    	}
    	printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
    	printf("\n\n-------------------------------------------------------");
    	printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", number);
    	printf("\n\n-------------------------------------------------------");
    	  
    	getch();
    	return 0;
    }
    I'm able to count the number of string. :cool: but I'm still confuse with the space counting? would you help me out on the code? :(

IMN logo majestic logo threadwatch logo seochat tools logo