Define a class to accept a string and print the same in reverse also print the number of vowels in the string

Share post:

String manipulation question is one of the topic in almost all interviews

Define a class to accept a string and print the same in reverse, also print the number of vowels in the string.

Complete question

Question 7

Define a class to accept a string and print the same in reverse, also print the number of

vowels in the string.

Eg : S= "BEAUTIFUL"

Output -

"LUFITUAEB"

No. of vowels = 5

 

package com.sp;

public class PrintReverse {
//Define a class to accept a string and print the same in reverse, also print the number of vowels
public static void main(String[] args) {
String s="BEAUTIFUL";
int length = s.length();
char[] reverse= new char[length];
length=length-1;
int vow=0;
for(char ch:"BEAUTIFUL".toCharArray()){
reverse[length]=ch;
length=length-1;
if("AEIOU".contains(String.valueOf(ch).toUpperCase())){
vow++;
}

}
System.out.println(s+"->"+String.valueOf(reverse));
System.out.println("Vowels:"+vow);
}
}

 

Smashplus

Insights

Share post: