Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package otherfiles.com;
- import javax.swing.JOptionPane;
- class StrinfImplementation {
- private String word;
- public StrinfImplementation() {
- }
- public String getWord() {
- return word;
- }
- public void setWord(String word) {
- this.word = word;
- }
- public String reverse() {
- char[] ch = word.toCharArray();
- String w = " ";
- int spacecnt = 0;
- // count the isWhitespace
- for (char c : ch) {
- if (Character.isWhitespace(c)) {
- spacecnt++;
- }
- }
- int i, j = ch.length - 1;
- int size = word.length() - spacecnt;
- if (size % 2 == 0) {// if size if multiple of 2
- for (i = 0; i < j; i++, j--) {
- char temp = ch[i];
- ch[i] = ch[j];
- ch[j] = temp;
- }
- }
- w = String.valueOf(ch);
- return w;
- }
- public int[] count(){
- int[] ans = new int[5];
- for (int j = 0; j < word.length(); j++) {
- char c = word.toLowerCase().charAt(j);
- if (c == 'a') {
- ans[0] += 1;
- } else if(c == 'e') {
- ans[1] += 1;
- } else if(c == 'i') {
- ans[2] += 1;
- } else if(c == 'o') {
- ans[3] += 1;
- } else if(c == 'u') {
- ans[4] += 1;
- }
- }
- return ans;
- }
- public String indent() {
- String spaces = " ";
- int vowelCount = 0;
- String newWord = "";
- for(char c : word.toCharArray()) {
- newWord += String.valueOf(c);
- if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
- c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
- vowelCount++;
- }
- if(vowelCount == 5) {
- newWord += spaces;
- vowelCount = 0;
- }else {
- continue;
- }
- }
- return newWord;
- }
- }
- public class JavaApp {
- public static void main(String[] args) {
- StrinfImplementation im = new StrinfImplementation();
- //System.out.println("Enter a string: ");
- String word = JOptionPane.showInputDialog("Enter a string:");
- im.setWord(word);
- String str = im.getWord();
- int[] vowelcnt = im.count();
- System.out.println("Enter a string: "+ str);
- System.out.println("Reverse: "+ im.reverse());
- System.out.println("Count: a = "+vowelcnt[0] + ", e = " +vowelcnt[1] + ", i = " +vowelcnt[2] + ", o = " +vowelcnt[3] + ", u = " +vowelcnt[4]);
- System.out.println("Indent: "+im.indent());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement