#include<stdio.h>
#include<string.h>

int count(char string[]);

void main(){

	int i;
	char string[1001];

	printf("String: \n");
	gets(string);

	i = words(string);

	printf("No. of Words %d", i);
}

int count(char string[])
{
  int i, j, k;

  for(i = 0; i < strlen(string); i++){
    if(string[i] == ' ')
		k += 1;
	else
		j += 1;
  }
	return j; 
}

I have spent a few hours searching for something that would help me count words (not letters) in a string the user types. There can be more than one space between each word. I have seen instances of using inbuilt function but I cant use them for this assignment.

Any help would be appreciated, thanks in advance.

I did try to do that but for some reason my output is wrong, I get a 6 digit output for a four word string I put in.

#include<stdio.h>
#include<string.h>

int count(char string[]);

void main(){

	int i;
	char string[1001];

	printf("String: \n");
	gets(string);

	i = words(string);

	printf("Words %d", i);
}

int count(char string[])
{
  int i, j, k;

  for(i = 0; i < strlen(string); i++){
    if(string[i] == ' ')
		k += 1;
	else
		j += 1;
  }
	return j; 
}

This is the code I have written so far.

What you could use...is strtok

Oops

I have seen instances of using inbuilt function but I cant use them for this assignment.

>void main(){
It's int main ( void ) , and you have to return a value (0 is a common choice). No excuses.

>gets(string);
You took the time to make your array nice a big with extra room for a null character at 1001, and then destroyed all of that work by using gets. gets doesn't give a hoot about the size of your array, it'll just happily write until it finds a newline or EOF, even if you type 50,000 characters or redirect a 2GB file full of Qs.

>for(i = 0; i < strlen(string); i++)
This is a sneak attack by the quadratic army. Don't call strlen in your loop condition because it walks over the entire string looking for '\0' every single time. Save the result unless the size of the string is going to change during the loop.

>gets(string);
You took the time to make your array nice a big with extra room for a null character at 1001, and then destroyed all of that work by using gets. gets doesn't give a hoot about the size of your array, it'll just happily write until it finds a newline or EOF, even if you type 50,000 characters or redirect a 2GB file full of Qs.

After reading that explanation if you are asking yourself "what can I do, then"; here's some links that shine light in the matter:
Things to Avoid in C/C++ -- gets()
Safe Version of gets()
Read a Line of Text from the User
Read a Line of Text from the User, Discard Excess Characters
Beloved fgets()

#include<stdio.h>


int main()
{

char name[100],ch;
int i,count=0,flag;

printf("Enter the strings\n");
gets(name);

for(i=0;name!='\0';i++)
{
if(name=='m')
flag=0;
if(flag==0)
{
flag=1;
count++;
}
}

printf("Total number of occurance of character %d\n",count);

}

Comments
Fail.

This program is not affected when the user types more than one space or any symbol used in sentences, or tab etc.
PROGRAMER MD. AZAZ VESIT MUmbai

#include <stdio.h>
         void CountWords(void);
         void main()
         {
            printf("\n\tcount the words and enter string\n\n\n");
            CountWords();

         }
        void CountWords(void)
        {
            char c;
            int num=0;
            int flag= 0;
             while((c=getchar())!='\n')
            {
			if((c==' ')||(c=='	')||(c=='.')||(c==';')||(c==',')||(c==';')
				||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
              {
                flag=0 ;        
              }                         
              else if(flag==0)
              {
                num++;
                flag=1;     
              }
			  
           }
              printf("\t\n\n\nNumber of words is %d\n",num);
         }
This article has been dead for over six months. Start a new discussion instead.