• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP For Search a Letter In a String

WAP For Search a Letter In a String

Last Updated on: August 2, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Search a Letter In a String

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:

  1. Import Statements: The program starts without any import statements, as it doesn’t require any external classes from the Java standard library.
  2. 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.
  3. 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.
  4. 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.
  5. 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!

    Real Time Java Program Interview Questions
    Top 70 Java Program Interview Questions
    WAP to Format a Phone Number in Human-Readable View
    WAP to Format a Phone Number in Human-Readable View
    Leap Year In Java
    LeapYear In Java With Example Program
    WAP to Check Character Sequence Is in Same Order Or Not (isomorphic)
    WAP to Check Character Sequence Is in Same Order Or...
    Convert Binary To Decimal
    Convert Binary To Decimal
    WAP to Move Digits to Left and Characters to Right
    WAP to Move Digits to Left and Characters to Right
    WAP to Sort Array Of String According To String Length
    WAP to Sort Array Of String According To String Length
    Product of Array Except Itself Program
    Product of Array Except Itself
    Print the Preceding Character By the Value
    Write a Java Program to Print the Preceding Character By...
    Fibonacci Series Numbers Sequence Program
    Fibonacci Series Numbers Sequence Program

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers