Source Code
package packaga;
public class longestWord {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String str="Example String to test";
String greatest="";
int s=str.length(),wordSize=0;
String[] wordsCollecton=str.split(" "); // Splits the "Hello pak world" in to 3 words [ Hello , pak , world ]
for(String tempWord:wordsCollecton)//iterate for each word
{
if(tempWord.length()>wordSize)//find the greater word
{
greatest=tempWord;//stores the greater word
wordSize = tempWord.length();//stores the size
}
}
System.out.println("Longest Word is \""+greatest+"\" in String \""+str+"\"");
}
}
Output of the Program
package packaga;
public class longestWord {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String str="Example String to test";
String greatest="";
int s=str.length(),wordSize=0;
String[] wordsCollecton=str.split(" "); // Splits the "Hello pak world" in to 3 words [ Hello , pak , world ]
for(String tempWord:wordsCollecton)//iterate for each word
{
if(tempWord.length()>wordSize)//find the greater word
{
greatest=tempWord;//stores the greater word
wordSize = tempWord.length();//stores the size
}
}
System.out.println("Longest Word is \""+greatest+"\" in String \""+str+"\"");
}
}
Output of the Program
0 comments:
Post a Comment