Search a Letter In a String: Welcome to the world of Java programming, where we embark on an exciting journey of searching for a specific letter within a given string! In this Java Programs Examples, we’ll explore the power of algorithms and string manipulation as we dive into the task of locating a particular letter’s presence in a text. Whether you’re a beginner eager to learn the fundamentals of Java or an experienced coder seeking to sharpen your problem-solving skills, this program caters to all levels.
Throughout this exploration, we’ll unravel the step-by-step process of designing an efficient solution to this search challenge. We’ll delve into Java’s syntax and explore techniques to optimize our code for speed and simplicity. By understanding the logic behind our program, you’ll gain insights into algorithm design, conditional statements, and character handling in Java.
Join us as we learn how to navigate through strings, detect characters, and present the results of our search. From its practical applications in text analysis to enhancing your programming prowess, this Java program is a fantastic opportunity to expand your knowledge and appreciation of the Java language. Let’s get ready to embark on this exciting coding adventure and discover the art of searching a letter in a string!
Search a Letter In a String
package com.softwaretestingo.interviewprograms; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InterviewPrograms94 { //Write a program for removing white spaces in a String. public static void main(String[] args) throws IOException { String str; char c; // create an object of Scanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the string"); str=br.readLine(); System.out.println("Enter the Character you want to search"); c=(char)br.read(); int index = str.indexOf(c); System.out.printf(c +" is present at index %d", index); } }
Output
Enter the string SoftwareTestingo Enter the Character you want to search e e is present at index 7
This Java program is designed to search for a specific character in a given string. Let’s walk through the code step by step:
- Import Statements: The program starts without any import statements, as it doesn’t require any external classes from the Java standard library.
- Class and Method Declaration: The program defines a class named InterviewPrograms94. Inside the class, there is the main method, which serves as the entry point of the program.
- User Input and Declaration: In the main method, the program prompts the user to enter a string using System.out.println(). The user’s input is obtained using a BufferedReader, which reads input from the console. The input string is then stored in the str variable.The program also prompts the user to enter a character they want to search for in the string. The user’s input is obtained using another BufferedReader, and the character is stored in the c variable.
- Searching for the Character: The program uses the indexOf method of the String class to find the first occurrence of the character c in the input string str. The result is stored in the index variable.
- Output: Finally, the program prints the index of the first occurrence of the character c in the input string using System.out.printf().
In summary, this Java program efficiently searches for a specific character within a given string and displays the index of its first occurrence. It demonstrates how to use the indexOf method to find a character in a string and interact with user inputs. Java beginners can learn from this code about handling user input, character manipulation, and performing search operations within a string. Happy coding!
Leave a Reply