Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.
Question
Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.
Solution
Sure! Here's a step-by-step guide to writing a Java program that counts the number of words with the first and last letter being the same:
-
Start by creating a new Java class. You can name it something like "WordCount".
-
Declare a public class named "WordCount" and include a main method inside it.
-
Inside the main method, create a Scanner object to accept user input. You can use the following code to do this:
Scanner scanner = new Scanner(System.in); -
Prompt the user to enter a string and store it in a variable. You can use the following code to do this:
System.out.print("Enter a string: "); String inputString = scanner.nextLine(); -
Split the input string into an array of words using the
split()method. You can split the string by spaces using the following code:String[] words = inputString.split(" "); -
Initialize a variable to keep track of the count of words with the first and last letter being the same. You can set it to 0 initially:
int count = 0; -
Iterate over each word in the array and check if the first and last characters are the same. If they are, increment the count variable. You can use the following code to do this:
for (String word : words) { if (word.charAt(0) == word.charAt(word.length() - 1)) { count++; } } -
Finally, print the count of words with the first and last letter being the same. You can use the following code to do this:
System.out.println("Number of words with the first and last letter being the same: " + count); -
Close the Scanner object to free up system resources. You can use the following code to do this:
scanner.close(); -
Save and run the program. You can now enter a string and the program will count the number of words with the first and last letter being the same.
That's it! You have successfully written a Java program to count the number of words with the first and last letter being the same.
Similar Questions
Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same int len= ; System.out.println("The length of the word is "+) ; }}
This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".
Write a program that asks the user to input two strings: an input string and a"pattern" string. Both strings contain only lowercase alphabet, and the"pattern" string is shorter than the input string. The program finds and countsnumber of occurrences of the "pattern" string in the input string, assuming thatthe number of occurrences is less than 10. The program replaces the firstcharacter of the first appearance of the "pattern" string in the input string by '1'and the first character of the second appearance of the "pattern" string in theinput string by '2' and so on so forth. Finally, the program outputs two strings:the original input string and the modified string, a string in a line, ended withnewline.
Raghu's tutor provides him a simple sentence and asks him to count the number of words in the sentence. The words are separated by space.Example, if the tutor provides the sentence as "Welcome to Java", the output should beNumber of words 3The words are WelcometoJavaFill the space in the below code so that the code fulfills his requirement. Code : Scanner sc=new Scanner(System.in); String str=sc.nextLine(); String words[ ] = ; int count= ; //count holds the number of words in the sentence System.out.println("Number of words "+); System.out.println("The words are "); for(String : ) //for each loop System.out.println(x);
Develop a Java program with a method for comparing two strings.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.