| | |
help with c program to count # of words in a string
![]() |
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Solved Threads: 0
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
I had the same assignement 1 month ago 
Here's the code :
@ Windsurfer : If there's something you don't understand ,feel free to reply
Good luck

Here's the code :
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define SIZE 100 int count(char vector[],int nr); int main() { int i,string_size,x; char string[SIZE]; printf("Type a text: "); gets(string); string_size=strlen(string); x=count(string,string_size); printf("Number of words: %d\n",x); return 0; } 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; }
Good luck
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;
}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.
•
•
•
•
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.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; }
Hope it helped, bye.

You were right , it's much easier and simple with isalpha()
Here's the new version:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> #include <string.h> int main() { char string[100]; int i,found=0; printf("Type a string : "); gets(string); for(i=0;i<strlen(string);i++) if( isalpha(string[i])) found++; printf("Number of characters: %d\n",found); return 0; }
Thanks ~s.o.s~
"Get rich or die tryin"(50-cent)
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( ).
[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.
@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( ).
c Syntax (Toggle Plain Text)
char buffer[BUFSIZ] = { '\0' } ; // set character array to all null chars printf( "Enter string: " ) ; fgets( buffer, BUFSIZ, stdin ) ; //
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.
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:
@ 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)
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Converting array of chars to int
- Next Thread: Problem Printing the whole argv[4]
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






