shivamisone

longest shortest word ez

Jul 6th, 2023
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | Source Code | 0 0
  1. import java.util.*;
  2. public class breh{
  3.     public static void main(){
  4.         Scanner sc= new Scanner(System.in);
  5.         String k = sc.nextLine();
  6.         k = k+" ";
  7.         int j =0;
  8.         String[] ar = new String[k.length()];
  9.         int[] ar1 = new int[k.length()];
  10.         String word = "";
  11.         for(int i =0;i<k.length();i++){
  12.             word =word+k.charAt(i);
  13.             if(k.charAt(i) == ' '){
  14.                 ar[j] = word;
  15.                 ar1[j] = word.length();
  16.                 word = "";
  17.                 j++;
  18.             }
  19.         }
  20.         System.out.println("longest:"+Arrays.toString(ar));
  21.         System.out.println("longest:"+Arrays.toString(ar1));
  22.         for(int b =0;b<ar.length;b++){
  23.         for(int i =0; i<k.length()-1;i++){
  24.             if(ar1[i+1]>ar1[i]){
  25.                 int t = ar1[i];
  26.                 ar1[i] = ar1[i+1];
  27.                 ar1[i+1] = t;
  28.                 String tt = ar[i];
  29.                 ar[i] = ar[i+1];
  30.                 ar[i+1] = tt;
  31.             }
  32.         }}
  33.         System.out.println("smallest:"+ar[j-1]);
  34.         System.out.println("longest:"+ar[0]);
  35.         System.out.println("longest:"+Arrays.toString(ar));
  36.         System.out.println("longest:"+Arrays.toString(ar1));
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment