patterncModerate
Basic Word Counter in C
Viewed 0 times
wordcounterbasic
Problem
Working on a C programming question where I have to write a function which will determine how many words are in a given string. Assume that one or more consecutive white spaces is a delimiter between words, and that the string you pass to your function is null terminated.
Have to use pointers and only the #include(stdio.h) library. Wondering how this can be improved upon or if there are any possible errors. Here it is:
Have to use pointers and only the #include(stdio.h) library. Wondering how this can be improved upon or if there are any possible errors. Here it is:
#include
int word_counter(char string[])
{
//We start with first word unless we have a empty string then we have no words
int count;
if(*string!='\0'){
count=1;
}
else{
count=0;
return 0;
}
//while we dont reach the end of the string
while(*string!='\0'){
//if we detect a whitespace
if(*string==' '){
//get previous character
string--;
// If previous character is not a space we increase the count
// Otherwise we dont since we already counted a word
if(*string!=' '){
count++;
}
//return pointer to current character
string++;
}
// set pointer to next character
string++;
}
return count;
}
//just to test if it works
int main(void)
{
char str[] = "Hello World!";
printf("How many words? = %i\n", word_counter(str));
return 0;
}Solution
isspaceThere are other characters besides for spaces. What happens if I do:
Helloworld!Your code will report that there is one word. I would rewrite these:
if(*string==' '){
//get previous character
string--;
// If previous character is not a space we increase the count
// Otherwise we dont since we already counted a word
if(*string!=' '){
count++;
}You should instead use
isspace for these kind of things. ' ' is for explicitly a space.Indentation
Fix your indentation. Your
main uses 4 spaces, your word_counter uses (maybe?) 2. Be sure that it is consistent. Choose one or the other.Empty case rework
(Actually, as @200_success points out you don't need this corner case. I'm going to leave this up here though, because sometimes you will get cases like this and you should consider reworking them if they appear awkward)
Your empty corner case can be reworked:
int count;
if(*string!='\0'){
count=1;
}
else{
count=0;
return 0;
}First you don't need to set
count = 0 if you just return immediately. I would restructure you if statement to be:if (*string == '\0') {
return 0;
}And from there continue with:
int count = 1;This means we don't leave
count uninitialized either.Code Snippets
Hello<tab><tab>world!if(*string==' '){
//get previous character
string--;
// If previous character is not a space we increase the count
// Otherwise we dont since we already counted a word
if(*string!=' '){
count++;
}int count;
if(*string!='\0'){
count=1;
}
else{
count=0;
return 0;
}if (*string == '\0') {
return 0;
}int count = 1;Context
StackExchange Code Review Q#159378, answer score: 12
Revisions (0)
No revisions yet.