Computer Applications
Write a program in java to accept a string and display the new string after reversing the characters of each word and converting first letter of each word to upper case.
Sample Input:
the tea is hot
Sample Output:
ehT aeT sI toH
Answer
import java.util.Scanner;
public class KboatStringReverseWord
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String str = in.nextLine();
str = str + " ";
String word = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
int x = word.length();
for (int j = x - 1; j >= 0; j--) {
char ch2 = word.charAt(j);
if (j == 0) {
System.out.print(Character.toUpperCase(ch2));
}
else {
System.out.print(ch2);
}
}
System.out.print(" ");
word = "";
}
else {
word += ch;
}
}
}
}Output
Related Questions
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
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.