Advertisement
Kulas_Code20

ReverseAndVowels

Sep 28th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package otherfiles.com;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. class StrinfImplementation {
  6.  
  7.     private String word;
  8.  
  9.     public StrinfImplementation() {
  10.  
  11.     }
  12.  
  13.     public String getWord() {
  14.         return word;
  15.     }
  16.  
  17.     public void setWord(String word) {
  18.         this.word = word;
  19.     }
  20.  
  21.     public String reverse() {
  22.         char[] ch = word.toCharArray();
  23.         String w = " ";
  24.         int spacecnt = 0;
  25.         // count the isWhitespace
  26.         for (char c : ch) {
  27.             if (Character.isWhitespace(c)) {
  28.                 spacecnt++;
  29.             }
  30.         }
  31.         int i, j = ch.length - 1;
  32.         int size = word.length() - spacecnt;
  33.         if (size % 2 == 0) {// if size if multiple of 2
  34.             for (i = 0; i < j; i++, j--) {
  35.                 char temp = ch[i];
  36.                 ch[i] = ch[j];
  37.                 ch[j] = temp;
  38.             }
  39.         }
  40.         w = String.valueOf(ch);
  41.         return w;
  42.     }
  43.    
  44.     public int[] count(){
  45.         int[] ans = new int[5];
  46.         for (int j = 0; j < word.length(); j++) {
  47.             char c = word.toLowerCase().charAt(j);
  48.             if (c == 'a') {
  49.                 ans[0] += 1;
  50.             } else if(c == 'e') {
  51.                 ans[1] += 1;
  52.             } else if(c == 'i') {
  53.                 ans[2] += 1;
  54.             } else if(c == 'o') {
  55.                 ans[3] += 1;
  56.             } else if(c == 'u') {
  57.                 ans[4] += 1;
  58.             }
  59.         }
  60.         return ans;
  61.     }
  62.    
  63.     public String indent() {
  64.          String spaces = "     ";
  65.          int vowelCount = 0;
  66.          String newWord = "";
  67.  
  68.          for(char c : word.toCharArray()) {
  69.             newWord += String.valueOf(c);
  70.             if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
  71.                     c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
  72.                 vowelCount++;
  73.             }
  74.             if(vowelCount == 5) {
  75.                newWord += spaces;
  76.                 vowelCount = 0;
  77.             }else {
  78.                 continue;
  79.             }
  80.          }
  81.         return newWord;
  82.     }
  83. }
  84.  
  85. public class JavaApp {
  86.     public static void main(String[] args) {
  87.         StrinfImplementation im = new StrinfImplementation();
  88.        
  89.         //System.out.println("Enter a string: ");
  90.         String word = JOptionPane.showInputDialog("Enter a string:");
  91.        
  92.         im.setWord(word);
  93.         String str = im.getWord();
  94.         int[] vowelcnt = im.count();
  95.        
  96.         System.out.println("Enter a string: "+ str);
  97.         System.out.println("Reverse: "+ im.reverse());
  98.         System.out.println("Count: a = "+vowelcnt[0] + ", e = " +vowelcnt[1] + ", i = " +vowelcnt[2] + ", o = " +vowelcnt[3] + ", u = " +vowelcnt[4]);
  99.         System.out.println("Indent: "+im.indent());
  100.        
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement