Computer Applications
Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.
Answer
import java.util.Scanner;
public class KboatString
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String str = in.nextLine();
str = str.toUpperCase();
str += " ";
int len = str.length();
String word = "";
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
char first = word.charAt(0);
char last = word.charAt(word.length() - 1);
if (first == last) {
System.out.println(word);
}
word = "";
}
else {
word += ch;
}
}
}
}
Output
Related Questions
Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
The output of a program which extracts a part of the string "SUBMISSION" is as follows:
(a) "MISS"
(b) "MISSION"If
String str = "SUBMISSION";
write appropriate Java statements to get the above outputs.Which of the following returns a value greater than or equal to 0?
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)