0

I'm a beginner programmer and im trying to solve some exercises and i needed some help with one of them. The exercise goes like this:

We need to input a string of characters , read it and print out the length of each word.

This is what i did

int main()
{
char str[N+1+1];
int i=0;
int pos=0;
int wordlen=0;
int word[60]={0,};
printf("Please enter the string of characters: ");
gets(str);
while(i<strlen(str))
{
    if(!isalpha(str[i]))
    {
        wordlen=0;
        i++;
    }
    if(isalpha(str[i]))
    {
        wordlen++;
        i++;
        pos=i;
    }
    word[pos]=wordlen;
    wordlen=0;;
    i++;
}
for(i=0;i<20;i++)
{
    if(word[i]==0) // here im just trying to find a way to avoid printing 0's but you can ignore it if you want
        {break;}
    else
        printf("%d ",word[i]);
}
  return 0;
}

The problem is that when i try to compile it for example: I input "hi hi hi" its supposed to print 2 2 2 but instead it's printing nothing.

Can i ask for some help?

  • Do basic debugging. Step through the code in a debugger and/or add more debug print statements to trace the program flow. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – kaylum Oct 25 '21 at 22:40
  • 2
    OT: [Never use gets](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – kaylum Oct 25 '21 at 22:41
  • 1
    @kaylum we are supposed to use gets. Its its mentioned in the exercise i just thought it wasnt necesary to post it. And as for the debugging ive been trying for the last 30 or so min. I just cant figure out where the problem lies – Severjan Lici Oct 25 '21 at 22:43
  • Ok, so share with us what you found in your debugging. For example, what is the resulting contents of the `word` array and is it what is expected? If not, why not? – kaylum Oct 25 '21 at 22:44
  • @kaylum So my line of thought was: I will start checking every word letter by letter. If the first letter is not a character i will move on to the next and the length will be 0. The moment i find a letter i will keep counting how many letters there are and store that into another array. then once im out i will reset the wordlength to 0 and repeat the process. I think in my mind its an OK solution. And that is what ive been trying to put into the compiler but to no avail. – Severjan Lici Oct 25 '21 at 22:48
  • 1
    You didn't actually answer my questions. Debugging involves finding out where things first start going wrong. So for example, are the `word` contents correct? If they are not then work backwards from there if they are work forwards. – kaylum Oct 25 '21 at 22:51
  • SeverjanLici, "but instead it's printing nothing." --> Because `word[0]` is zero. Print all 20 `word[i]` for better debug info. – chux - Reinstate Monica Oct 26 '21 at 08:04

1 Answers1

1

I failed to follow OP's logic.

Perhaps begin again?

End-of-word

To "count the length of each word", code needs to identify the end of a word and when to print.

Detecting a non-letter and the current word length > 0 indicates the prior character was the end of a word. Note that every C string ends with a non-letter: '\0', so let us iterate on that too to insure loop ends on a final non-letter.

int word_length = 0;
int strlength = strlen(str);  // Call strlen() only once
while (i <= strlength) {
  if (isalpha(s[i])) {
    word_length++;
  } else {
    if (word_length > 0) {
      printf("%d ", word_length); 
      word_length = 0;
    }
  }
}
printf("\n"); 

gets()

gets() is no longer in the C library for 10 years as it is prone to over-run. Do not use it.

we are supposed to use gets. is unfortunate and implies OP’s instruction is out-of-date. Instead, research fgets() and maybe better instruction material.

Advanced

is...() better called as isalpha((unsigned char) s[i]) to handle s[i] < 0.

In general, better to use size_t than int for string sizing and indexing as the length may exceed INT_MAX. That is not likely to happen with OP's testing here.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • as far as i remember the fgets should be for folders right? I have some knowledge about that but im starting again from the beginning and as for why i used the gets() function we are instructed to do so in the beginning of the exercise. I suppose its a bad idea to use it in more complex problmes but i dont think it will be that much of a problem in simple ones. Tho i will take your and the others warning but at the same time i have to do as the exercise instructs me to. Thanks for the help – Severjan Lici Oct 26 '21 at 09:40
  • @SeverjanLici "should be for folders right? " --> `fgets(...,..., stdin)` is fine. – chux - Reinstate Monica Oct 26 '21 at 09:44