Reputation: 465
This is my first post on stack overflow :) I didn't find relevant post to my issue despite numerous posts on "counting words".
I started C 2 weeks ago. I have to return the number of words in a string, this is part of a larger exercise I m working on at the moment. I can't figure out why it doesn't work and I am kindly asking for some tips here.
ft_strlen(char *str) //counting nb of char in the string
{
int size;
size = 0;
while (str[size])
size++;
return (size);
}
int ft_word_count(char *str)
{
int i;
int size;
int count_word;
i = 0;
size = ft_strlen(str);
count_word = 0;
while (str[i] < size - 1) //counting nb of words in the string, I added "-1" to size to get rid of the '\0'
{
if (i <= 32 || i > 126 ) //defining what will make a word
count_word++;
i++;
}
return (count_word);
}
int main(void)
{
char str[]="Meine Frau liebt grosse Pferde";
ft_strlen(str);
printf("%d", ft_word_count(str));
return (0);
}
it returns 0 instead of 5, strangely, don't figure out why. If I just use my strlen, it returns "30", as expected. So something is wrong with ft_word_count
Compiled with gcc. Syntax is not concise but is part of the norm asked by my school.
thanks for your input
Charles
Upvotes: 1
Views: 786
Reputation: 145317
There are multiple problems in your code:
while (str[i] < size - 1)
is incorrect as you are comparing the value f the character to the size of the string instead of the index: it should be while (i < size)
.
if (i <= 32 || i > 126 )
is incorrect: it is not a proper way to check for word separators as non ASCII characters will not be considered part of the word and the encoding might not be ASCII anyway. You should instead use isspace()
from <ctype.h>
.
Furthermore, counting the spaces is not a way to count the words. You should instead count the number of transitions from space to non-space.
Here is a simpler version:
#include <ctype.h>
#include <stdio.h>
int ft_word_count(const char *str) {
unsigned char c, last = ' ';
int count = 0;
for (int i = 0; (c = str[i]) != '\0'; i++) {
if (!isspace(c) && isspace(last))
count++;
last = c;
}
return count;
}
int main(void) {
char str[] = "Meine Frau liebt grosse Pferde";
printf("%d\n", ft_word_count(str));
return 0;
}
Upvotes: 0
Reputation: 11
you should ignore multiple spaces for counting correct
i=0;
count_word=0;
while(str[i]>0)
{
if((str[i]!= ' '))
{
if(!toggle && str[i]!= ' ')
count_word++;
toggle=1;
}
else
toggle=0;
i++;
}
Upvotes: 1
Reputation: 221
Error Part
while (str[i] < size - 1)
Here its checking with the ascii value at that place of string which will always be false and hence loop is not running.
Correct Way
while (i < size - 1)
{
if (str[i] == ' ')
count_word++;
i++;
}
count_word++;
Upvotes: 0
Reputation: 44368
The problem is these lines
while (str[i] < size - 1) // Here you compare the individual chars and
// the length of the string. That makes
// no sense
{
if (i <= 32 || i > 126 ) // Here you compare the index i and some
// fixed numbers. That makes no sense
count_word++;
i++;
}
It seems you have swapped the two, i.e. you use str[i]
when you should use i
and you use i
when you should use str[i]
So if you change your code to:
while (i < size - 1)
{
if (str[i] <= 32 || str[i] > 126 )
count_word++;
i++;
}
You'll see that things start to make more sense. That code will print 4
. That is still wrong but now you have some code that you can continue with.
A simple approach could be:
while (i < size - 1)
{
if (str[i] == ' ')
count_word++;
i++;
}
count_word++;
That code will print 5
. However, the code is too simple as it count double spaces as words.
In other words - you need to add more code to handle such case but I guess that is part of the learning process. Good luck.
Upvotes: 0
Reputation: 16379
I believe that you meant to use logic more like this:
if(str[i] <= 32 || str[i] > 126) count_word++;
In the code that you posted, you are looking at the value of your index, not the character value in the string.
Even so, this is not why you are receiving "0" as a result. The cause of this is your while
condition. You are checking to see if the numeric ASCII value within the string is greater than the length of the string... which I can assure you, it is. Therefore, you also want to change your white to be:
while(i < size - 1)
Personally, I would likely have checked for \n, space and \t instead, but to each his own!
Upvotes: 0