help with c program to count # of words in a string

Reply

Join Date: Oct 2004
Posts: 16
Reputation: Alfy is an unknown quantity at this point 
Solved Threads: 0
Alfy Alfy is offline Offline
Newbie Poster

help with c program to count # of words in a string

 
0
  #1
Oct 12th, 2004
I am kinda new to world of c programming, and i have been assigned a problem that is written as follows: Write a procedure that counts the number or words in a string. Write a program to test your new procedure. I am kinda lost on this completely. Any help on how to define the number of words by the amount of spaces. How do i define a word? Where do i want to start. I figure it will involve some form of looping and a counter. Any help will be greatly appreciated
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 133
Reputation: Cup of Squirrel is an unknown quantity at this point 
Solved Threads: 1
Cup of Squirrel's Avatar
Cup of Squirrel Cup of Squirrel is offline Offline
Junior Poster

Re: help with c program to count # of words in a string

 
0
  #2
Oct 12th, 2004
This sounds like a simple string. I know nothing about C, but I know you'll need a string or function for this.

Look through any docs or manuals you can find. It would be something like count$() but dont rely on me, im a newbie too
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 16
Reputation: Alfy is an unknown quantity at this point 
Solved Threads: 0
Alfy Alfy is offline Offline
Newbie Poster

Re: help with c program to count # of words in a string

 
0
  #3
Oct 12th, 2004
thanks, i think i figured it out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: Windsurfer is an unknown quantity at this point 
Solved Threads: 0
Windsurfer Windsurfer is offline Offline
Newbie Poster

Re: help with c program to count # of words in a string

 
0
  #4
Dec 2nd, 2006
Originally Posted by Alfy View Post
thanks, i think i figured it out.
great. Can you post the code, so others can learn?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: help with c program to count # of words in a string

 
0
  #5
Dec 2nd, 2006
I had the same assignement 1 month ago
Here's the code :
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define SIZE 100
  5.  
  6. int count(char vector[],int nr);
  7.  
  8. int main()
  9.  
  10. {
  11. int i,string_size,x;
  12. char string[SIZE];
  13. printf("Type a text: ");
  14. gets(string);
  15. string_size=strlen(string);
  16. x=count(string,string_size);
  17. printf("Number of words: %d\n",x);
  18.  
  19. return 0;
  20. }
  21.  
  22. int count (char vector[],int nr)
  23.  
  24. {
  25. int gasit=0,i;
  26. for(i=0;i<nr;i++)
  27. {
  28. if(vector[i]>='a' && vector[i]<='z') gasit++;
  29. if(vector[i]>='A' && vector[i]<='Z') gasit++;
  30. }
  31.  
  32. return gasit;
  33.  
  34. }
@ Windsurfer : If there's something you don't understand ,feel free to reply

Good luck
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with c program to count # of words in a string

 
0
  #6
Dec 2nd, 2006
int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

}
I think there is a better way of doing this, just use the [search]isalpha( )[/search] function which checks whether the given character is an alphabet or not. You require ctype.h header file to use this function.

Hope it helped, bye.
Last edited by ~s.o.s~; Dec 2nd, 2006 at 2:30 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: help with c program to count # of words in a string

 
0
  #7
Dec 2nd, 2006
Originally Posted by ~s.o.s~ View Post
int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

}
I think there is a better way of doing this, just use the [search]isalpha( )[/search] function which checks whether the given character is an alphabet or not. You require ctype.h header file to use this function.

Hope it helped, bye.
Very nice tip
You were right , it's much easier and simple with isalpha()
Here's the new version:
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. char string[100];
  8. int i,found=0;
  9.  
  10. printf("Type a string : ");
  11. gets(string);
  12.  
  13. for(i=0;i<strlen(string);i++)
  14. if( isalpha(string[i]))
  15. found++;
  16. printf("Number of characters: %d\n",found);
  17.  
  18. return 0;
  19. }

Thanks ~s.o.s~
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with c program to count # of words in a string

 
0
  #8
Dec 2nd, 2006
ha ha, gets(string);
Newbie.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with c program to count # of words in a string

 
0
  #9
Dec 2nd, 2006
Originally Posted by iamthwee View Post
ha ha, gets(string);
Newbie.
Ah... looks like it missed my eye.

@Eko:
Hey there buddy, normally its a bad programming practice to use the function gets( ) since it does no bound checking i.e. even if you declare your string to be consisting of 10 characters, nothing prevents you from entring 100 characters. This will result in buffer overflow and writign to memory that doenst belong to you which in the end leads to corruption.

You are better off using fgets( ).
  1. char buffer[BUFSIZ] = { '\0' } ; // set character array to all null chars
  2. printf( "Enter string: " ) ;
  3. fgets( buffer, BUFSIZ, stdin ) ; //
[search]fgets( )[/search]
The above is a much safer way of accpeting strings or a matter of fact any input from the user.

Dont forget to remove the newline character which is present at the string end.
Last edited by ~s.o.s~; Dec 2nd, 2006 at 4:25 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: help with c program to count # of words in a string

 
0
  #10
Dec 2nd, 2006
Offtopic :
@ iemtwhee I don't know what u find so funny ; if you are so "advanced" , go write a c programming book or something :lol:

@~s.o.s~ Thanks for the support,i heared about fgets(),but our teachers at the university want us to use the gets() function and if u don't do it their way,than is the highway :lol:
Last edited by Eko; Dec 2nd, 2006 at 4:30 pm.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC