C Program to Find Largest Word in String

C Program to Find Largest Word in String

Working with strings is one of the most important skills in C programming. Strings are used to store and manipulate text, and learning how to analyze them opens the door to many real-world applications such as text processing, file handling, and data parsing. One interesting problem beginners often encounter is finding the largest word in a string. This exercise teaches how to traverse a string, handle word boundaries, and perform comparisons, which are essential skills for any budding programmer.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Understanding how to extract and compare words also strengthens your grasp of loops, arrays, and conditional statements. Whether you are working on a small utility or preparing for programming challenges, learning how to identify the largest word in a string is a practical and rewarding exercise. In this article, we’ll explore beginner-friendly programs to solve this problem step by step.

Program 1: Find Largest Word in a Predefined String

This program demonstrates how to find the largest word in a string that is predefined in the code.

#include <stdio.h>
#include <string.h>

int main() {

    char str[] = "Learning C programming is fun and exciting";
    char largest[50], word[50];
    int maxLen = 0;

    int i = 0, j = 0;

    while (str[i] != '\0') {

        if (str[i] != ' ') {
            word[j++] = str[i];
        } else {

            word[j] = '\0';

            if (j > maxLen) {
                maxLen = j;
                strcpy(largest, word);
            }

            j = 0;

        }

        i++;

    }

    // Check last word
    word[j] = '\0';

    if (j > maxLen) {
        strcpy(largest, word);
    }

    printf("The largest word is: %s\n", largest);

    return 0;

}

In this program, we traverse the string character by character. Each word is temporarily stored in the word array, and its length is compared to the maximum length found so far. If it’s larger, we store it as the largest word. Finally, we print the largest word after checking the last word in the string. This program helps beginners understand string traversal, handling spaces as delimiters, and using the strcpy() function for copying words.

Program 2: Find Largest Word from User Input

This program allows the user to enter a string and then finds the largest word.

#include <stdio.h>
#include <string.h>

int main() {

    char str[200], largest[50], word[50];
    int maxLen = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    int i = 0, j = 0;

    while (str[i] != '\0' && str[i] != '\n') {

        if (str[i] != ' ') {
            word[j++] = str[i];
        } else {

            word[j] = '\0';

            if (j > maxLen) {
                maxLen = j;
                strcpy(largest, word);
            }

            j = 0;

        }

        i++;

    }

    // Check last word
    word[j] = '\0';

    if (j > maxLen) {
        strcpy(largest, word);
    }

    printf("The largest word is: %s\n", largest);

    return 0;

}

Here, fgets() is used to read a line of text from the user, which allows the program to handle spaces correctly. The logic to find the largest word is the same as in the previous program. Beginners can see how input handling differs from predefined strings and how the same algorithm can be applied dynamically.

Program 3: Using strtok() to Find Largest Word

This program demonstrates how to use the C standard library function strtok() to split the string into words and find the largest one.

#include <stdio.h>
#include <string.h>

int main() {

    char str[200];
    char largest[50];
    int maxLen = 0;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    str[strcspn(str, "\n")] = '\0'; // Remove newline character
    char *token = strtok(str, " ");

    while (token != NULL) {

        if (strlen(token) > maxLen) {
            maxLen = strlen(token);
            strcpy(largest, token);
        }

        token = strtok(NULL, " ");

    }

    printf("The largest word is: %s\n", largest);

    return 0;

}

Using strtok(), we can split the string into tokens based on a delimiter (space in this case). Each token is then compared for its length, and the largest one is stored. This method is easier and more elegant for beginners because it avoids manual character-by-character traversal and clearly shows how C library functions can simplify common tasks.

Frequently Asked Questions (FAQ)

Finding the largest word in a string raises some common beginner questions.

Q1. What if the string has punctuation?
Punctuation can be treated as part of the word, or you can remove it before processing.

Q2. Can this handle multiple spaces between words?
Yes, the programs handle consecutive spaces, but additional checks can improve robustness.

Q3. What happens if two words are equally largest?
The programs return the first largest word encountered.

Q4. Can we find the largest word in a file?
Yes, by reading each line from a file and applying the same logic to each line.

Q5. What is the advantage of strtok() over manual traversal?
strtok() simplifies string splitting and reduces code complexity, making it easier to read and maintain.

Conclusion

Finding the largest word in a string is an excellent exercise for beginners to understand string manipulation, array handling, and working with library functions. Whether you work with predefined strings, user input, or use strtok(), each method teaches valuable concepts that are widely applicable in programming challenges, text processing, and real-world applications. Practicing these programs strengthens your understanding of strings and enhances your problem-solving skills in C programming.

Additional & References

For beginners who want to explore strings and word manipulation further:

  1. C Standard Library (string.h) – Documentation for strlen(), strcpy(), and strtok() functions.
  2. Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
  3. GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
  4. Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.
Scroll to Top