Find the Longest word in a string

 Problem statement:- Program to Find the Longest word in a string.

Example:-

              Input: Given

                                 String=Find the Longest word in string

             Output: Longest Substring(Word) is Longest. 

            Input: Given

                                 String=C++ string questions

             Output: Longest Substring(Word) is questions

Data requirement:-


   Input Data:- str

  Output Data:-
sub_str

  Additional Data:- 
in,out,p,len1,maxInd,max

Program in C

Here is the source code of the C Program to Find the Longest word in a string.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    int in=0,out=0,p=0,len1,maxInd=0,max=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;
    max=strlen(sub_str[0]);
    //finding max length of Substring from splitting string length

        for(in=0;in<len;in++)
        {
            len1=strlen(sub_str[in]);
            if(len1>max)
            {
                max=len1;
                maxInd=in;
            }
        }
        printf("Longest Substring(Word) is %s\n",sub_str[maxInd]);
}

Input/Output:
Enter your String:Find the Longest word in a string
Longest Substring(Word) is Longest

Program in C++

Here is the source code of the C++ Program to Find the Longest word in a string.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    int in=0,out=0,p=0,len1,maxInd=0,max=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;
    max=strlen(sub_str[0]);
    //finding max length of Substring from splitting string length
        for(in=0;in<len;in++)
        {
            len1=strlen(sub_str[in]);
            if(len1>max)
            {
                max=len1;
                maxInd=in;
            }
        }
        cout<<"Longest Substring(Word) is "<<sub_str[maxInd]<<"\n";
}

Input/Output:
Enter your String:C++ string questions
Longest Substring(Word) is questions

Program in Java

Here is the source code of the Java Program to Find the Longest word in a string.

Code:

import java.util.Scanner;
public class LongestSubstring {

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,maxInd=0,max=0;
    
  //finding max length of Substring from splitting string length
    int len=sub_str.length;
    max=sub_str[0].length();
        for(in=0;in<len;in++)
        {
            len1=sub_str[in].length();
            if(len1>max)
            {
                max=len1;
                maxInd=in;
            }
        }
       System.out.print("Longest Substring(Word) is "+sub_str[maxInd]+"\n");
    cs.close();
}
}

Input/Output:
Enter your String:
string program in java 
Longest Substring(Word) is program

Program in Python

Here is the source code of the Python Program to Find the Longest word in a string.

Code:

str=input("Enter Your String:")
sub_str=str.split(" ")
maxInd=0
max=0

max = len(sub_str[0])
for inn in range(0,len(sub_str)):
    len1 = len(sub_str[inn])
    if len1 > max:
        max=len1
        maxInd=inn
print("Longest Substring(Word) is ",sub_str[maxInd])

Input/Output:
Enter Your String:Python String Program
Longest Substring(Word) is  Program

Post a Comment

0 Comments