In the given string, find the longest and shortest word

Task given: In the given string, find the longest and shortest word. Display them on the screen and display their length.

The longest word outputs, the shortest is not, it does not work, for the length of the words I did not even try to output. I hope for help, for the 5th hour I'm breaking my brain

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    int main()
    {
        char string[200];

        printf("Input a sentences: ");

        gets(string);

        char * pch = strtok (string," "),
             * maxword = 0,
             * minword = 0;

        int length = strlen(pch);

        int maxLen = 0;
        int minLen = 1000;

          while (pch != NULL)
          {
              length = strlen(pch);

              if (maxLen < length )
              {
                  maxLen = length;
                  maxword = pch;
              }

              pch = strtok (NULL, " ");
          }

          while (pch != NULL)
          {
              length = strlen(pch);

              if (minLen > length )
              {
                  minLen = length;
                  minword = pch;
              }

              pch = strtok (NULL, " ");
          }

          for(char *pch = strtok(string, " "); pch; pch = strtok(NULL, " "))
        {
            if(length < minLen)
            {
                minLen = length;
                minword = pch;
                strcpy(string,pch);
            }
        }
          printf("The longest word is: %s\n", maxword);
          printf("The least word is: %s\n", minword);

        return 0;
    }
    `
 1
c
Author: Ainar-G, 2019-11-14

2 answers

  1. It's not clear why you would expect this to work at all. After completing the first loop while (searching for a long string), the value pch will be equal to NULL. The second loop while (search for a short string), of course, will simply be skipped in this situation.

  2. As I understand it, instead of dealing with the second loop, you wrote a third loop for that tries to do what the second loop while couldn't do...

    But the function strtok is destroying function-it modifies the original string by replacing some characters with \0. After such a replacement, the original string can no longer be processed by repeating the same strtok.

    Therefore, all your hopes that you will be able to go through the words of the line through strtok several times are doomed to failure.

If you want all your loops to run through strtok, then you will have to provide each such loop with the original, an undamaged copy of the string. But it would be better to do without numerous passes along the line. To solve this problem, one pass through the source line is more than enough.

P.S. The C standard library no longer has the gets function.

 0
Author: AnT, 2019-11-14 23:26:32

You have several errors in your code, both factual and stylistic, but the answer to your question can be reduced to the fact that you do in three cycles what is done in one:

for (char *word = strtok(s, delims); word != NULL;
     word = strtok(NULL, delims)) {
    size_t len = strlen(word);

    if (len > max_len) {
        max_len = len;
        max_word = word;
    }

    if (len < min_len) {
        min_len = len;
        min_word = word;
    }
}

Full working code:

#include <stdint.h>

// size_t
// SIZE_MAX

#include <stdio.h>

// feof
// fflush
// fgets
// perror
// printf

#include <stdlib.h>

// NULL

#include <string.h>

// strtok
// strlen

enum { SIZE = 200 };

static const char delims[] = "\t\n\r ";

int main()
{
    char s[SIZE];

    printf("enter a sentence: ");
    fflush(stdout);
    char *res = fgets(s, SIZE, stdin);
    if (!res && !feof(stdin)) {
        perror("can't get the sentence");
        return 1;
    }

    char *max_word = NULL;
    char *min_word = NULL;

    size_t max_len = 0;
    size_t min_len = SIZE_MAX;

    for (char *word = strtok(s, delims); word != NULL;
         word = strtok(NULL, delims)) {
        size_t len = strlen(word);

        if (len > max_len) {
            max_len = len;
            max_word = word;
        }

        if (len < min_len) {
            min_len = len;
            min_word = word;
        }
    }

    printf("the longest word is: %s\n", max_word);
    printf("the shortest word is: %s\n", min_word);

    return 0;
}
 0
Author: Ainar-G, 2019-11-14 23:56:23