Computer Science, asked by siddharthasingh15, 7 months ago

Wap in java to enter string and display shortest word and length of shortest word.​

Attachments:

Answers

Answered by lagu786
0

Explanation:

import java.util.*;

class stringcheck

{

public static void main()

{

Scanner sc=new Scanner(System.in);

System.out.println("enter the sentence");

String st=sc.nextLine();

int L=st.length();

char ch;

int k=0,arr[ ]=new int[0] , c=0;

for(int i=0;i<L;i++)

{

ch=st.charAt(i);

k++;

if(ch == ' ')

{

arr[c++]=k;

k=0;

}

}

int max=arr[0];

int Len=arr.length;

for(int i=0;i<Len;i++)

{

if(max < arr[i] )

{

max = arr[i];

}

}

System.out.print("lenght of largest word="+max);

}

}

HOPE THIS WILL HELP U

Answered by Vyomsingh
2

Question:-

Wap in java to enter string and display shortest word and length of shortest word.

Code:-

import java.io.BufferedReader;

class FindMinMaxString

{

public static void main(String args[])

{

findMethod("My name is Vyomsingh");

}

static public void findMethod(String s)

{

String str = s + " ";

char ch = ' ';

int len = str.length(), l = 0;

int min = len;

String shortest_word = "", word = "";

for (int i = 0; i < len; i++)

{

ch = str.charAt(i);

if (ch != ' ')

{

word += ch;

} //if ends

else

{

l = word.length();

if (l < min)

{

min = l;

shortest_word = word;

} //if ends

word = "";

}

}

System.out.println("Shortest word = " +shortest_word + " with length " + min);

}

}

Input :

My name is Vyomsingh

Output:

Shortest word = My with length 2

Similar questions