write a java program that does the following: I. prompts the user for a phrase.
ID: 3539371 • Letter: W
Question
write a java program that does the following:
I. prompts the user for a phrase. for example " the quick brown fox jumped obver the lazy dog."
II. print the pharse backwards. for example, if enters " hello there", your program prints" hello there reversed is ereht olleh"
III. determines and prints the number of constants, and number of vowels, in the phrase the user enters.
IV determines and prints if the pharse entered is a palindrome. that is whether it reads the same forwards and backwards, ignoring the case of the letters. for example, the word "madam" is palindrome.
V repeats step 1 to 4 until the user enters "QUIT" when promted for a phrase.
Explanation / Answer
import java.io.* ;
import java.util.* ;
public class test {
public static void main(String args[])
{while(true)
{System.out.println("Please Enter a phrase:");
Scanner s=new Scanner(System.in);
String phrase=s.nextLine();
String g="QUIT";
int b=g.compareTo(phrase);
if(b==0)
break;
StringBuffer sb=new StringBuffer(phrase);
System.out.println("the reversed string is: "+sb.reverse());
int l=phrase.length();
int vowel_count=0,consonent_count=0;
for(int i=0;i<l;i++)
{char c=phrase.charAt(i);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'||c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
vowel_count++;
else if(c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' || c != ' '||c != 'A' || c != 'E' || c != 'I' || c != 'O' || c != 'U')
consonent_count++;
}
System.out.println("the number of vowel in string is: "+vowel_count);
System.out.println("the number of consonent in string is: "+consonent_count);
String str = sb.toString();
phrase=phrase.toUpperCase();
str=str.toUpperCase();
int x = str.compareTo(phrase);
if(x==0)
System.out.println("the string is palindrome");
else
System.out.println("the string is not palindrome");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.