Write a JAVA program to check weather a char exist within string or not. example :- str1="hello" str2="e" output :- Your search string is Found at 2 place.

import java.util.*;
class SearchString 
{
        //declaration of str1 and str2
String str1,str2;
        //constructor
SearchString()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter 1st string:- ");
                //accept 1st string through Scanner object (s)
str1=s.nextLine();
System.out.print("Enter string which you want to search in 1st string :- ");
//accept search string through Scanner object (s)
                str2=s.nextLine();
}
void check()
{
//indexOf is a method of string which  return index position of first occurance
int i=str1.indexOf(str2);
if(i==-1)
{
System.out.println("Your search string is not found.");
}
else
{
i++;
System.out.println("Your search string is Found at "+i+" place.");
}
}
}
class TestSearchString
{
public static void main(String args[])
{
                //Creating an instance of class SearchString
SearchString obj=new SearchString();
                //calling of function
obj.check();  
}

0 Comments