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:
- 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
Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
Write a program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000
Write a Java program to find the smallest window in a string containing all characters of another string.
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.
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.