This is a C program to find the largest & smallest word in a string.
This C Program finds the largest and smallest possible word in a string.
This program finds the largest and smallest possible word in a string as shown in the program below.
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; }
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.
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.
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.
$ 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
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Buy Computer Science Books
- Practice BCA MCQs
- Apply for C Internship
- Watch Advanced C Programming Videos