How to print the longest word of a string in C

How to print the longest word of a string in C:

In this post, we will learn how to print the longest word of a string in C. The program will take one string as input from the user and print the longest word of the string.

With this post, you will learn how to read a string in C, how to iterate through the characters of a string and how to find and print the longest word.

C program:

Let’s write down the program first:

#include <stdio.h>

int main()
{
    int length = 0;
    int longestLength = 0;
    int endIndex = 0;
    int i, j;

    char str[100];
    char word[100];

    // read the string
    printf("Enter a string:\n");
    scanf("%[^\n]", str);

    // iterate through the characters
    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] != ' ')
        {
            length++;
            continue;
        }

        if (length > longestLength)
        {
            longestLength = length;
            endIndex = i;
        }

        length = 0;
    }

    if (length > longestLength)
    {
        longestLength = length;
        endIndex = i;
    }

    // copy the longest word
    j = 0;
    for (i = endIndex - longestLength; i < endIndex; i++, j++)
    {
        word[j] = str[i];
    }
    word[j] = '\0';

    printf("Longest word: %s\n", word);

    return 0;
}

Here,

  • length is a variable to hold the current length of the word. The program will iterate through the characters of the string and this variable will hold the length of the current word.
  • longestLength is to hold the length of the longest word and endIndex is to hold the index of the character just after the end character of the longest word. So the longest word will start at index endIndex - longestLength and ends at endIndex - 1
  • i and j are two variables to use in the loops.
  • str and word are two character arrays to hold the user input string and the longest word.
  • It uses printf to ask the user to enter a string and by using scanf, it reads the string and stores that in the character array str.
  • The for loop iterates through the characters of the array one by one.
    • If the current character is a blank character, it increases the value of length by 1. The continue statement moves it to the next iteration and ignores the next steps.
    • If the current character is not a blank character, i.e. it reaches the end of a word. It checks if the length of the word calculated is more than the longest length or not. If yes, assign the current value as the longest length, assign the current value of i to endIndex.
    • Also, reset the value of length to 0.
  • After the for loop ends, compare the value of length one more time with longestLength. This check is for the last word of the string. If length is greater than longestLength, assign the value of length to longestLength and assign i to endIndex.
  • The next for loop is used to copy the longest word from the user input string str to the string word. It starts from endIndex - longestLength and ends at endIndex - 1. It copies all characters from str to word.
  • The final printf statement prints the longest word, i.e. word.

Output:

Let’s see how it works:

Enter a string:
hello universe
Longest word: universe

Enter a string:
hello world and universe
Longest word: universe

Enter a string:
hello a ab abc abcd !!
Longest word: hello

C print longest word in string

As you can see here, it prints the longest word each time.

Conclusion:

In this post, we learned how to print the largest word of a string. You can use the same algorithm with a little bit change to find the smallest word of a string. If you want to find the smallest word, you need to compare the current word length with a value that is used to store the smallest value. Similar to the above example, we can copy the word to a different string.