Problem statement:- Program to Find the shortest word in a string.
Example:-
Input: Given
String=Find the shortest word in string
Output: Smallest Substring(Word) is in.
Input: Given
String=C++ string questions
Output: Smallest Substring(Word) is C++
Data requirement:-
Input Data:- str
Output Data:-sub_str
Additional Data:- in,out,p,len1,minInd,min
Program in C
Here is the source code of the C Program to Find the shortest word in a string.
Code:
#include<stdio.h>
#include<string.h>
main()
{
int in=0,out=0,p=0,len1,minInd=0,min=0;
char str[100]={0},sub_str[100][100]={0};
printf("Enter your String:");
gets(str);
//splitting Input String into sub string
while(str[p]!='\0')
{
out=0;
while(str[p]!=' '&&str[p]!='\0')
{
sub_str[in][out]=str[p];
p++;
out++;
}
sub_str[in][out]='\0';
in++;
if(str[p]!='\0')
{
p++;
}
}
int len=in;
min=strlen(sub_str[0]);
//finding min length of Substring from splitting string length
for(in=0;in<len;in++)
{
len1=strlen(sub_str[in]);
if(len1<min)
{
min=len1;
minInd=in;
}
}
printf("Smallest Substring(Word) is %s\n",sub_str[minInd]);
}
Enter your String:Find the shortest word in string
Smallest Substring(Word) is in
Program in C++
Here is the source code of the C++ Program to Find the shortest word in a string.
Code:
#include<iostream>
#include <cstring>
using namespace std;
main()
{
int in=0,out=0,p=0,len1,minInd=0,min=0;
char sub_str[100][100]={0};
string str;
cout<<"Enter your String:";
getline(cin,str);
//splitting Input String into sub string
while(str[p]!='\0')
{
out=0;
while(str[p]!=' '&&str[p]!='\0')
{
sub_str[in][out]=str[p];
p++;
out++;
}
sub_str[in][out]='\0';
in++;
if(str[p]!='\0')
{
p++;
}
}
int len=in;
min=strlen(sub_str[0]);
//finding min length of Substring from splitting string length
for(in=0;in<len;in++)
{
len1=strlen(sub_str[in]);
if(len1<min)
{
min=len1;
minInd=in;
}
}
cout<<"Smallest Substring(Word) is "<<sub_str[minInd]<<"\n";
}
Enter your String:C++ string questions
Smallest Substring(Word) is C++
Program in Java
Here is the source code of the Java Program to Find the shortest word in a string.
Code:
import java.util.Scanner;
public class SmallestSubstring {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
str1+=" ";
//splitting Input String into sub string
String[] sub_str=str1.split("\\s");
int in,len1,minInd=0,min=0;
//finding min length of Substring from splitting string length
int len=sub_str.length;
min=sub_str[0].length();
for(in=0;in<len;in++)
{
len1=sub_str[in].length();
if(len1<min)
{
min=len1;
minInd=in;
}
}
System.out.print("Smallest Substring(Word) is "+sub_str[minInd]+"\n");
cs.close();
}
}
Enter your String:
string program java
Smallest Substring(Word) is java
Program in Python
Here is the source code of the Python Program to Find the shortest word in a string.
Code:
str=input("Enter Your String:")
sub_str=str.split(" ")
minInd=0
min=0
min = len(sub_str[0])
for inn in range(0,len(sub_str)):
len1 = len(sub_str[inn])
if len1 < min:
min=len1
minInd=inn
print("Smallest Substring(Word) is ",sub_str[minInd])
Enter Your String:Python String Program
Smallest Substring(Word) is Python
0 Comments
Please do not Enter any spam link in the comment box