C Program
#include <stdio.h> #include <string.h> #include <limits.h> int main() { char str[200]; printf("Enter a sentence: "); fgets(str, sizeof(str), stdin); char *word = strtok(str, " \n"); char shortest[200]; int minLen = INT_MAX; while (word != NULL) { int len = strlen(word); if (len < minLen) { minLen = len; strcpy(shortest, word); } word = strtok(NULL, " \n"); } printf("Shortest word: %s\n", shortest); return 0; }
C Output
Input: this is a programming test Output: a
C++ Program
#include <iostream> #include <sstream> #include <climits> using namespace std; int main() { string str; cout << "Enter a sentence: "; getline(cin, str); string word, shortest; int minLen = INT_MAX; stringstream ss(str); while (ss >> word) { if ((int)word.length() < minLen) { minLen = word.length(); shortest = word; } } cout << "Shortest word: " << shortest << endl; return 0; }
C++ Output
Input: find the smallest word here Output: the
JAVA Program
import java.util.Scanner; public class ShortestWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a sentence: "); String str = sc.nextLine(); String[] words = str.split("\\s+"); String shortest = words[0]; for (String w : words) { if (w.length() < shortest.length()) { shortest = w; } } System.out.println("Shortest word: " + shortest); } }
JAVA Output
Input: Java is fun to learn Output: is
Python Program
sentence = input("Enter a sentence: ").split() shortest = min(sentence, key=len) print("Shortest word:", shortest)
Python Output
Input: Python makes coding easy Output: is
Explanation
Example
When we pass "this is a programming test", the program iterates over each word individually. It compares their lengths and stores the shortest word encountered so far. In this scenario, "a" has one character, thus it is the shortest. Likewise, in "find the smallest word here", the word "the" is shortest as it consists of only three letters.
Real-Life Analogy
Imagine it as when you compare things in the supermarket. If you're instructed to select the smallest fruit in a basket, you would compare sizes of each one, and you keep in mind the smallest ones until you're done. Getting the shortest word in a sentence is exactly the same thing—we compare each word for lengths, and we keep the shortest.
Why It Matters
This exercise instructs on decomposing text into words and examining them. It presents string tokenization and iteration principles, fundamental in natural language processing (NLP). Real-world applications such as search engines, spell checkers, and chatbots frequently have to operate at the word level on the text. The determination of the shortest or longest word is an early step toward more sophisticated text analysis.
Learning Insights
You learn to break a sentence into words, equate their attributes, and hold a "current best candidate" while looping. This technique is a building block for solving larger problems such as the most frequent word, longest palindrome in a sentence, or keyword detection. It also solidifies knowledge of strings, arrays, and conditionals.
Interview Relevance
String manipulation problems are frequently asked by interviewers because they test both logic as well as knowledge about existing functions. An example problem like "shortest word" can be seemingly straightforward but tests the candidate's skill in parsing the input, dealing with edge conditions such as consecutive spaces, and comparing in an efficient manner. Solution in Python using min(sentence, key=len) also demonstrates awareness about higher-order functions.
Practical Applications
Such logic can be used in text abstraction, in writing readability-checking software or highlighting software that highlights small but prominent words. As an instance, the shortest words (such as "if", "or", "by") in processing legal agreements can be crucial connectors. In competitive programming, such a problem gains confidence with string manipulation prior to processing more complicated parsing problems.
In summary, discovering the shortest word in a sentence is not just an entry-level string problem. It enhances problem-solving ability, refines knowledge of string parsing, and has practical uses in text analysis, search engines, and data cleaning. For students and employment seekers, such problem mastery assists in both coding interviews and practical programming projects that entail the handling of text. By mastering the implementation of this in C, C++, Java, and Python, you also know how strings and input are treated differently by various languages, which makes you a more flexible programmer.
Social Plugin