Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- public class shorthand{
- public static void main (String [] agrs){
- shorthand stringultil = new shorthand();
- System.out.println(stringultil.problem3("and"));
- } // end of main method
- public static String problem3 (String str){
- str = str.toLowerCase(); // making string lowercase
- String empty = ""; // making an empty string
- for(int i = 0; i <str.length(); i++){
- int indexAnd = str.indexOf("and");
- int indexTo = str.indexOf("to");
- int indexYou = str.indexOf("you");
- int indexFor = str.indexOf("for");
- // code for replacing word
- // finding the word and
- if(indexAnd == 0) {
- if(str.charAt(indexAnd + 3) == ' '){
- String test = str.substring(indexAnd, indexAnd + 2);
- test = "&";
- empty += test;
- }
- } else if(indexAnd != 0){
- if(str.charAt(indexAnd - 1) == ' ' || str.charAt(indexAnd + 3) == ' '){
- String test2 = str.substring(indexAnd, indexAnd + 2);
- test2 = "&";
- empty += test2;
- }
- }
- // finding the word to
- if(indexTo == 0) {
- if(str.charAt(indexTo + 2) == ' '){
- String test3 = str.substring(indexTo, indexTo + 1);
- test3 = "2";
- empty += test3;
- }
- } else if(indexTo != 0){
- if(str.charAt(indexTo - 1) == ' ' || str.charAt(indexTo + 2) == ' '){
- String test4 = str.substring(indexTo, indexTo + 1);
- test4 = "2";
- empty += test4;
- }
- }
- // finding the word you
- if(indexYou == 0) {
- if(str.charAt(indexYou + 3) == ' '){
- String test5 = str.substring(indexYou, indexYou + 2);
- test5 = "u";
- empty += test5;
- }
- } else if(indexYou != 0){
- if(str.charAt(indexYou - 1) == ' ' || str.charAt(indexYou + 3) == ' '){
- String test6 = str.substring(indexYou, indexYou + 2);
- test6 = "u";
- empty += test6;
- }
- }
- // finding the word for
- if(indexFor == 0) {
- if(str.charAt(indexFor + 3) == ' '){
- String test7 = str.substring(indexFor, indexFor + 2);
- test7 = "4";
- empty += test7;
- }
- } else if(indexAnd != 0){
- if(str.charAt(indexFor - 1) == ' ' || str.charAt(indexFor + 3) == ' '){
- String test8 = str.substring(indexFor, indexFor + 2);
- test8 = "4";
- empty += test8;
- }
- }
- // end of code for replacing word
- int asciiA = (int)(str.charAt(i));
- int asciiE = (int)(str.charAt(i));
- int asciiI = (int)(str.charAt(i));
- int asciiO = (int)(str.charAt(i));
- int asciiU = (int)(str.charAt(i));
- if(asciiA == 97 || asciiE == 101 || asciiI == 105 || asciiO == 111 || asciiU == 117) {
- if(i == 0){
- if( (str.charAt(1) >= 32) && (str.charAt(i) <= 64)){
- // leave letter alone
- }
- }else if(i != 0){
- if( (str.charAt(1) >= 97) && (str.charAt(i) <= 122)) {
- char ch = str.charAt(i);
- ch = ' ';
- empty += ch;
- }
- }
- }
- } // end of for-loop
- return empty;
- } // end of method
- } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement