# Program to Find the Largest and Smallest Word in a String

Program to Find the Largest and Smallest Word in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

## Program to find the largest & smallest word in a string

### Explanation

In this program, we need to find the smallest and the largest word present in the string.

```Hardships often prepare ordinary people for an extraordinary destiny
```

Consider above example in which 'an' is the smallest word and 'extraordinary' is the largest word. One of the approach to find smallest and largest word is to split string into words then, compare length of each word with variables small and large. If length of a word is less than length of small then, store that word in small. If length of a word is greater than length of large then, store that word in large.

### Algorithm

1. Define a string.
2. Convert the string to lowercase to make it case-insensitive.
3. Add an extra space at the end.
4. Now, iterate through the string till space is found and add those character into variable word. Add each word into words array. Array words will hold all the words present in the string.
5. Initialize variable small and large with first word of array.
6. Iterate through array words, check if the length of word is less than small. If yes, store that word in small.
7. If the length of word is greater than large. If yes, store that word in large.
8. At the end, display the smallest and largest word.

### Python

```string = "Hardships often prepare ordinary people for an extraordinary destiny";
word = "";
words = [];

#Add extra space after string to get the last word in the given string
string = string + " ";

for i in range(0, len(string)):
#Split the string into words
if(string[i] != ' '):
word = word + string[i];
else:
words.append(word);
#Make word an empty string
word = "";

#Initialize small and large with first word in the string
small = large = words[0];

#Determine smallest and largest word in the string
for k in range(0, len(words)):

#If length of small is greater than any word present in the string
#Store value of word into small
if(len(small) > len(words[k])):
small = words[k];

#If length of large is less than any word present in the string
#Store value of word into large
if(len(large) < len(words[k])):
large = words[k];

print("Smallest word: " + small);
print("Largest word: " + large);
```

Output:

```Smallest word: an
Largest word: extraordinary
```

### C

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

int main()
{
char string[] = "Hardships often prepare ordinary people for an extraordinary destiny";
char words[100][100], small[100], large[100];
int i = 0, j = 0, k, length;

//Split the string into words such that each row of array words represents a word
for(k=0; string[k]!='\0'; k++){

//Here, i represents row and j represents column of two-dimensional array words
if(string[k] != ' ' && string[k] != '\0'){
words[i][j++] = string[k];
}
else{
words[i][j] = '\0';
//Increment row count to store new word
i++;
//Set column count to 0
j = 0;
}
}

//Store row count in variable length
length = i + 1;

//Initialize small and large with first word in the string
strcpy(small, words[0]);
strcpy(large, words[0]);

//Determine smallest and largest word in the string
for(k = 0; k < length; k++){

//If length of small is greater than any word present in the string
//Store value of word into small
if(strlen(small) > strlen(words[k])){
strcpy(small, words[k]);
}
//If length of large is less than any word present in the string
//Store value of word into large
if(strlen(large) < strlen(words[k]))
strcpy(large, words[k]);
}

printf("Smallest word: %s\n", small);
printf("Largest word: %s", large);

return 0;
}
```

Output:

```Smallest word: an
Largest word: extraordinary
```

### JAVA

```public class SmallestLargestWord
{
public static void main(String[] args){
String string = "Hardships often prepare ordinary people for an extraordinary destiny";
String word = "", small = "", large="";
String[] words = new String[100];
int length = 0;

//Add extra space after string to get the last word in the given string
string = string + " ";

for(int i = 0; i < string.length(); i++){
//Split the string into words
if(string.charAt(i) != ' '){
word = word + string.charAt(i);
}
else{
words[length] = word;
//Increment length
length++;
//Make word an empty string
word = "";
}
}

//Initialize small and large with first word in the string
small = large = words[0];

//Determine smallest and largest word in the string
for(int k = 0; k < length; k++){

//If length of small is greater than any word present in the string
//Store value of word into small
if(small.length() > words[k].length())
small = words[k];

//If length of large is less than any word present in the string
//Store value of word into large
if(large.length() < words[k].length())
large = words[k];
}

System.out.println("Smallest word: " + small);
System.out.println("Largest word: " + large);
}
}
```

Output:

```Smallest word: an
Largest word: extraordinary
```

### C#

```using System;

public class SmallestLargestWord
{
public static void Main()
{
String string1 = "Hardships often prepare ordinary people for an extraordinary destiny";
String word = "", small = "", large="";
String[] words = new String[100];
int length = 0;

//Add extra space after string1 to get the last word in the given string
string1 = string1 + " ";

for(int i = 0; i < string1.Length; i++){
//Split the string into words
if(string1[i] != ' '){
word = word + string1[i];
}
else{
words[length] = word;
//Increment length
length++;
//Make word an empty string
word = "";
}
}

//Initialize small and large with first word in the string
small = large = words[0];

//Determine smallest and largest word in the string
for(int k = 0; k < length; k++){

//If length of small is greater than any word present in the string
//Store value of word into small
if(small.Length > words[k].Length)
small = words[k];

//If length of large is less than any word present in the string
//Store value of word into large
if(large.Length < words[k].Length)
large = words[k];
}

Console.WriteLine("Smallest word: " + small);
Console.WriteLine("Largest word: " + large);
}
}
```

Output:

```Smallest word: an
Largest word: extraordinary
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
\$string = "Hardships often prepare ordinary people for an extraordinary destiny";
\$word = "";
\$words = array();

//Add extra space after string to get the last word in the given string
\$string = \$string . " ";

for(\$i = 0; \$i < strlen(\$string); \$i++){
//Split the string into words
if(\$string[\$i] != ' '){
\$word = \$word . \$string[\$i];
}
else{
//Add word to string array words
array_push(\$words, \$word);
//Make word an empty string
\$word = "";
}
}

//Initialize small and large with first word in the string
\$small = \$large = \$words[0];

//Determine smallest and largest word in the string
for(\$k = 0; \$k < count(\$words); \$k++){

//If length of small is greater than any word present in the string
//Store value of word into small
if(strlen(\$small) > strlen(\$words[\$k]))
\$small = \$words[\$k];

//If length of large is less than any word present in the string
//Store value of word into large
if(strlen(\$large) < strlen(\$words[\$k]))
\$large = \$words[\$k];
}

print("Smallest word: " . \$small);
print("<br>Largest word: " . \$large);
?>
</body>
</html>
```

Output:

``` Smallest word: an
Largest word: extraordinary
```

Next Topic#