Computer Applications
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.
Java
Java String Handling
12 Likes
Answer
import java.util.Scanner;
public class KboatWordFrequency
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
System.out.println("Enter a word:");
String ipWord = in.nextLine();
str += " ";
String word = "";
int count = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
if (str.charAt(i) == ' ') {
if (word.equalsIgnoreCase(ipWord))
count++ ;
word = "";
}
else {
word += str.charAt(i);
}
}
if (count > 0) {
System.out.println(ipWord + " is present " + count + " times.");
}
else {
System.out.println(ipWord + " is not present in sentence.");
}
}
}
Output
![BlueJ output of 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. BlueJ output of 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.](https://cdn1.knowledgeboat.com/img/java-programs/KboatStringWordCount-word-frequency-java-program.jpg)
Answered By
7 Likes
Related Questions
A 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); } }
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid) { String str = _______(a)_________; if(______(b)_______) { System.out.println("Special Discount Eligible: " + oid); } }