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.
Java
Java String Handling
4 Likes
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
![BlueJ output of 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. BlueJ output of 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.](https://cdn1.knowledgeboat.com/img/java-programs/KboatSentence1-java-program-same-first-last-letter-word.jpg)
Answered By
1 Like
Related Questions
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?
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@2024A university student's registration number follows the format:
<CourseCode><Year><CollegeCode><RollNumber>
where
Component Description CourseCode A 3-letter code representing the course (e.g., CSE for Computer Science, ECE for Electronics & Communication) Year The last two digits of the admission year. CollegeCode A 3-digit code representing the college RollNumber A 4-digit unique student roll number. Examples
Registration Number Course Code Admission Year College Code Roll Number CSE240011023 CSE 24 001 1023 ECE252104297 ECE 25 210 4297 ASE230277259 ASE 23 027 7259 Define a class that accepts a student's registration number as input, extracts the relevant details, and displays them in the specified format.
import java.util.Scanner; public class KboatStuRegNum { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter reg. no.: "); _______(1)_________ int l = n.length(); if (l != 12) { System.out.println("Invalid reg. no."); System.exit(0); } _______(2)_________ _______(3)_________ _______(4)_________ _______(5)_________ System.out.println("Course Code : " + cc); System.out.println("Admission Year : " + yr); System.out.println("College Code : " + cl); System.out.println("Roll Number : " + rNo); } }