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
Recommended Answers
Jump to PostThis 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 :D
Jump to PostI had the same assignement 1 month ago ;)
Here's the code :#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 …
Jump to Postint 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 …
Jump to Postint 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 …
Jump to Postha ha, gets(string);
Newbie.
All 26 Replies
Cup of Squirrel
0
Junior Poster
Alfy
0
Newbie Poster
Windsurfer
0
Newbie Poster
Eko
1
Junior Poster in Training
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
Eko
1
Junior Poster in Training
iamthwee
1,547
Banned
Featured Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
Eko
1
Junior Poster in Training
iamthwee
1,547
Banned
Featured Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
Eko
1
Junior Poster in Training
IwalkAlone
7
Light Poster
Aia
1,977
Nearly a Posting Maven
Salem
5,138
Posting Sage
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
IwalkAlone
7
Light Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
Talkin Tosh
0
Newbie Poster
Aia
1,977
Nearly a Posting Maven
nidhi.2384
0
Newbie Poster
ctype
-4
Newbie Poster
Salem
commented:
Showing up 3 YEARS LATE, with unformatted code (did you even read the intro threads), and using void main makes you a total waste of space
-4
jeunesse_doree1
0
Newbie Poster
Adak
419
Nearly a Posting Virtuoso
Birunda Devi
0
Newbie Poster
zeroliken
79
Nearly a Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.