Program to Find the Largest and Smallest Word in a String

«
»

This is a C program to find the largest & smallest word in a string.

Problem Description

This C Program finds the largest and smallest possible word in a string.

Problem Solution

This program finds the largest and smallest possible word in a string as shown in the program below.

advertisement
Program/Source Code

Here is a source code of the C program to find the largest and smallest possible word in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 * C Program to Find the Largest & Smallest Word in a String
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
    char string[100], word[20], max[20], min[20], c;
    int i = 0, j = 0, flag = 0;
 
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
 
    } while (c != '\n');
    string[i - 1] = '\0';
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
        {
            word[j++] = string[i++];
        }
        if (j != 0)
        {
            word[j] = '\0';
            if (!flag)
            {
                flag = !flag;
                strcpy(max, word);
                strcpy(min, word);
            }
            if (strlen(word) > strlen(max))
            {
                strcpy(max, word);
            }
            if (strlen(word) < strlen(min))
            {
                strcpy(min, word);
            }
            j = 0;
        }
    }
    printf("The largest word is '%s' and smallest word is '%s' in '%s'.\n", max, min, string);
 
    return 0;
}
Program Explanation

In this C program we are reading the character of the string using getchar() function. Assign the resulted value to ‘c’ character variable. Do while loop is used to assign the value of array of character to string variable. Check the condition that the value of ‘c’ character variable is equal to newline. If the condition is true then execute the statement. Assign the last value of array character as null.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

For loop is used to find the largest and smallest word in a string. While is used to check the condition by using the Logical AND operator that the value of ‘i’ variable is less than the given string length and should not have any space and it should not have any alphanumeric character. If the condition is true then execute the loop.

Assign the value of ‘string[]’ character array variable to a word array variable. Increment the value of ’j’ variable value. If condition statement is used to check the value of ‘j’ variable is not equal to zero. If the condition is true then assign the value of word array variable as null to that of the value of ‘j’ variable. Flag variable will hold only true (1) or false (0) value.

If condition statement is used to check the value of flag variable is opposite. That is if the condition is true means as false and false means true and vice versa. Assign the reverse value of the variable to ‘flag’ variable. The strcpy() is used to copy the string in word array character variable value to max variable and to the min variable.

advertisement

Another if condition is used to check the length of the string in word character array variable value is greater than the string present in the max array character variable. If the condition is true then execute the statement. Using strcpy() copy the string present in word character variable value to max character array variable.

Otherwise, if the condition is false execute another if condition statement. Check that the length of the string in word character array variable value is less than the string present in the min array character variable. If the condition is true, then execute the statement. Using strcpy() function, copy the string present in word character variable value to min character array variable.

Assign the value of ‘j’ variable to zero and exit the while loop statement. Print the statement as the largest and smallest word in the sentence. If else condition statement is used to check the value of ‘flag’ variable is equal to 1. If the condition is true then execute the statement and print the minimum and maximum palindrome in the given string. Otherwise, if the condition is false then execute the else statement and print the statement as the given string has no palindrome.

advertisement
Runtime Test Cases
 
$ gcc largestsmallest.c 
$ ./a.out
Enter string: amazing programmers exists here
The largest word is 'programmers' and smallest word is 'here' in 'amazing programmers exists here'.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.