Assume you have the following variable declarations: SimpleReader in = new Simpl
ID: 3666389 • Letter: A
Question
Assume you have the following variable declarations:
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
Write program fragments (i.e., you do not need to write complete programs) that read a line of input as a String and print:
Only the uppercase letters in the String
Every second letter of the String
The String with all vowels replaced by an underscore
The number of vowels in the String
The positions of all vowels in the String
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
Explanation / Answer
//to get the input string
out.print("Enter the input string");
String instring = in.next();
Only the uppercase letters in the String
for (int i=0;i<instring.length();i++){
if (Character.isUpperCase(instring.charAt(i))==true)
out.print(instring.charAt(i));
}
Every second letter of the String
for (int i=0;i<instring.length();i++){
if (i%2 == 1)
out.print(instring.charAt(i));
}
The String with all vowels replaced by an underscore
for (int i=0;i<instring.length();i++){
if (instring[i] == 'a' || instring[i] == 'e' || instring[i] == 'i' || instring[i] == 'o'|| instring[i] == 'u'
|| instring[i] == 'A' || instring[i] == 'E' || instring[i] == 'I' || instring[i] == 'O'|| instring[i] == 'U' )
out.print('_');
else
out.print(instring.charAt(i));
}
The number of vowels in the String
int vowel_count = 0
for (int i=0;i<instring.length();i++){
if (instring[i] == 'a' || instring[i] == 'e' || instring[i] == 'i' || instring[i] == 'o'|| instring[i] == 'u'
|| instring[i] == 'A' || instring[i] == 'E' || instring[i] == 'I' || instring[i] == 'O'|| instring[i] == 'U' )
vowel_count++;
}
out.print("Number of vowels in the string is : " + vowel_count );
The positions of all vowels in the String
out.println("The positions of vowels in the string are :");
for (int i=0;i<instring.length();i++){
if (instring[i] == 'a' || instring[i] == 'e' || instring[i] == 'i' || instring[i] == 'o'|| instring[i] == 'u'
|| instring[i] == 'A' || instring[i] == 'E' || instring[i] == 'I' || instring[i] == 'O'|| instring[i] == 'U' )
out.println(i+1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.