Find the smallest and the largest word present in the string
Example:
Input: Java is programming language
Output: smallest=is, largest=programming
Approach
Java
public class LargeAndSmallWord {public static void main(String[] args) {String str = "Java is programming language";String[] obj = str.split(" ");String small = "";String large = "";for (String string : obj) {if (small == "")small = string;// for largest word checkif (string.length() > large.length()) {large = string;}// small word checkif (small.length() > string.length())small = string;}System.out.println("smallest : " + small + ", largest : " + large);}}
Approach
Java
public class LargeAndSmallWord {public static void main(String[] args) {String str = "Java is programming language";String small = "";String large = "";String strCh = "";for (int i = 0; i < str.length(); i++) {char ch = str.charAt(i);if (!Character.isSpaceChar(ch)) {strCh = strCh + ch;continue;}if (small == "")small = strCh;// for largest word checkif (strCh.length() > large.length()) {large = strCh;}// small word checkif (small.length() > strCh.length())small = strCh;// resetstrCh = "";}System.out.println("smallest : " + small + ", largest : " + large);}}
No comments:
Post a Comment