import java.util.Scanner;
class StringExample {
public static String generateAdvertisement(String str) {
if (str.length() == 0)
throw new IllegalArgumentException("Please enter a valid phrase");
// split the input string into a list of words
String[] words = str.split(" ");
String newStr = "";
// loop through each word
for (int i = 0;i < words.length;i++) {
String currentWord = words[i];
// loop through each letter - execpt the first - always accept
// the first letter of a word, even if it is a vowel
newStr += currentWord.charAt(0);
for (int k = 1;k < currentWord.length();k++) {
char currentLetter = currentWord.charAt(k);
if (!isVowel(currentLetter))
newStr += currentLetter;
}
newStr += " ";
}
return newStr;
}
public static void prtc(String s) {
int index=1;
System.out.println("The length of s = " + s.length()+"\n\n");
if (s.length() > 0)
System.out.print(s.charAt(0)); // print the first character, if there is one
while (index < s.length()) {
if (s.charAt(index) == ' ') { // print the space, and next char if there is one
System.out.print(s.charAt(index));
if (index < s.length())
System.out.print(s.charAt(index+1));
index++; // skip the next character
}
else { // print constants only
if (!isVowel(s.charAt(index)))
System.out.print(s.charAt(index));
}
index++;
}
System.out.println();
}
public static boolean isVowel(char Letter) {
return Letter == 'a' ||
Letter == 'e' ||
Letter == 'i' ||
Letter == 'o' ||
Letter == 'u';
}
public static void meeeain(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Input the text you wish to Real Estatify");
System.out.println();
System.out.println("Example: ");
System.out.print("'Desirable unfurnished flat in quiet residential area' is: ");
System.out.println(generateAdvertisement("Desirable unfurnished flat in quiet residential area"));
System.out.println("\n Enter text now: ");
String input = keyboard.nextLine();
String output = generateAdvertisement(input);
System.out.println("Output is: \n" + output);
prtc(input);
}
public static void main(String[] args) {
String sentence = " Java string example ";
String word = "";
int len = sentence.length();
System.out.println("Example: sentence = " + sentence);
System.out.println("The length of sentence is " + sentence.length());
System.out.println();
sentence = sentence.trim();
System.out.println("The trimmed sentence = " + sentence);
System.out.println("The length of the trimed sentence " + sentence.length() + "\n");
word = sentence.substring(12);
System.out.println("\nsubstring of sentence beginning at character 12 = " + word + "\n");
word = sentence.substring(8,11);
System.out.println("\nsubstring(8,11) of sentence " + sentence + " = " + word + "\n");
int c = sentence.compareTo("a excellent example");
System.out.println("c = " + c + "\n");
String newSentence = sentence.concat(" of concatentation!");
System.out.println("newSentence = " + newSentence + "\n");
boolean e = sentence.equals("JAVA STRING EXAMPLE");
System.out.println("e = " + e + "\n");
boolean eIC = sentence.equalsIgnoreCase("JAVA STRING EXAMPLE");
System.out.println("EIC = " + eIC + "\n");
int i = sentence.indexOf("ing");
System.out.println("i = " + i+ "\n");
int lio = sentence.lastIndexOf("a");
System.out.println("lio = " + lio + "\n");
String lc = sentence.toLowerCase();
System.out.println("lc = " + lc + "\n");
String uc = sentence.toUpperCase();
System.out.println("uc = " + uc + "\n");
String rs = sentence.replace("a","A");
System.out.println("rs = " + rs + "\n");
int comp = 0;
String s1 = "This is the fist string";
String s2 = "zzzzzzzzz"; // lower case zzzz
String s3 = "AAAAAAA";
String s4 = "ZZZZZZZZ"; // upper case Z
comp = s1.compareTo(s2);
System.out.println("Compare " + s1 + " to " + s2 + " results: " + comp);
comp = s2.compareTo(s1);
System.out.println("Compare " + s2 + " to " + s1 + " results: " + comp);
comp = s2.compareTo(s3);
System.out.println("Compare " + s2 + " to " + s3 + " results: " + comp);
comp = s3.compareTo(s4);
System.out.println("Compare " + s3 + " to " + s4 + " results: " + comp);
comp = s4.compareTo(s4);
System.out.println("Compare " + s4 + " to " + s4 + " results: " + comp);
}
}
|