#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.

Recommended Answers

All 12 Replies

Member Avatar for iamthwee

Instead of using spaces to counts words (which won't work here) do the opposite perhaps?

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.

Member Avatar for iamthwee

Post the code you have thus far plz.

#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.

Member Avatar for iamthwee

No I meant post the code where you have tried the other way.

ALso a space isn't going to be a good method of word termination specification. Some people like to put two spaces after their full stops. Some people don't put spaces after comas etc. What you could use (though it probably is a bit overkill), is use strtok - http://www.cplusplus.com/reference/clibrary/cstring/strtok.html It's easy to modify to suit your purposes.

Member Avatar for iamthwee

What you could use...is strtok

Oops

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

Yeah ... So reading only the last post or two is not a great idea anymore.

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

}

commented: Fail. -4

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);
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.