Advertisement
cupertinoCSstruggle1

shorthand

Oct 30th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package test;
  2.  
  3. public class shorthand{
  4. public static void main (String [] agrs){
  5. shorthand stringultil = new shorthand();
  6. System.out.println(stringultil.problem3("and"));
  7. } // end of main method
  8.  
  9. public static String problem3 (String str){
  10. str = str.toLowerCase(); // making string lowercase
  11. String empty = ""; // making an empty string
  12.  
  13. for(int i = 0; i <str.length(); i++){
  14. int indexAnd = str.indexOf("and");
  15. int indexTo = str.indexOf("to");
  16. int indexYou = str.indexOf("you");
  17. int indexFor = str.indexOf("for");
  18.  
  19. // code for replacing word
  20.  
  21. // finding the word and
  22. if(indexAnd == 0) {
  23. if(str.charAt(indexAnd + 3) == ' '){
  24. String test = str.substring(indexAnd, indexAnd + 2);
  25. test = "&";
  26. empty += test;
  27. }
  28. } else if(indexAnd != 0){
  29. if(str.charAt(indexAnd - 1) == ' ' || str.charAt(indexAnd + 3) == ' '){
  30. String test2 = str.substring(indexAnd, indexAnd + 2);
  31. test2 = "&";
  32. empty += test2;
  33. }
  34. }
  35. // finding the word to
  36. if(indexTo == 0) {
  37. if(str.charAt(indexTo + 2) == ' '){
  38. String test3 = str.substring(indexTo, indexTo + 1);
  39. test3 = "2";
  40. empty += test3;
  41. }
  42. } else if(indexTo != 0){
  43. if(str.charAt(indexTo - 1) == ' ' || str.charAt(indexTo + 2) == ' '){
  44. String test4 = str.substring(indexTo, indexTo + 1);
  45. test4 = "2";
  46. empty += test4;
  47. }
  48. }
  49. // finding the word you
  50. if(indexYou == 0) {
  51. if(str.charAt(indexYou + 3) == ' '){
  52. String test5 = str.substring(indexYou, indexYou + 2);
  53. test5 = "u";
  54. empty += test5;
  55. }
  56. } else if(indexYou != 0){
  57. if(str.charAt(indexYou - 1) == ' ' || str.charAt(indexYou + 3) == ' '){
  58. String test6 = str.substring(indexYou, indexYou + 2);
  59. test6 = "u";
  60. empty += test6;
  61. }
  62. }
  63. // finding the word for
  64. if(indexFor == 0) {
  65. if(str.charAt(indexFor + 3) == ' '){
  66. String test7 = str.substring(indexFor, indexFor + 2);
  67. test7 = "4";
  68. empty += test7;
  69. }
  70. } else if(indexAnd != 0){
  71. if(str.charAt(indexFor - 1) == ' ' || str.charAt(indexFor + 3) == ' '){
  72. String test8 = str.substring(indexFor, indexFor + 2);
  73. test8 = "4";
  74. empty += test8;
  75. }
  76. }
  77.  
  78. // end of code for replacing word
  79.  
  80. int asciiA = (int)(str.charAt(i));
  81. int asciiE = (int)(str.charAt(i));
  82. int asciiI = (int)(str.charAt(i));
  83. int asciiO = (int)(str.charAt(i));
  84. int asciiU = (int)(str.charAt(i));
  85. if(asciiA == 97 || asciiE == 101 || asciiI == 105 || asciiO == 111 || asciiU == 117) {
  86. if(i == 0){
  87. if( (str.charAt(1) >= 32) && (str.charAt(i) <= 64)){
  88. // leave letter alone
  89. }
  90. }else if(i != 0){
  91. if( (str.charAt(1) >= 97) && (str.charAt(i) <= 122)) {
  92. char ch = str.charAt(i);
  93. ch = ' ';
  94. empty += ch;
  95. }
  96. }
  97. }
  98.  
  99.  
  100.  
  101. } // end of for-loop
  102. return empty;
  103. } // end of method
  104. } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement