-1

I made a program to search for a certain string in another string and print Word found if the condition is true or print word not found if condition is false

The logic is as follows

enter word length of word for searching for letter [1] if true then for till length of word match with string to be searched

else continue loop

But I always get word not found no matter what the input, please help me over here!!!

The code is as follows :-

    import java.util.Scanner;
    class Search_i_String
    {        
        public static void main(String args[])
        {
            int flag=0;
            Scanner Prakhar=new Scanner(System.in);
            System.out.println("Enter a String");
            String ori=Prakhar.nextLine();
            System.out.println("Enter the String to be Searched");
            String x=Prakhar.nextLine();
            char a[]=new char[ori.length()];
            char b[]=new char[x.length()];
            for(int i=0;i<ori.length();i++)
            {
                a[i]=ori.charAt(i);
            }
            for(int i=0;i<x.length();i++)
            {
                b[i]=x.charAt(i);
            }

            for(int i=0;i<a.length;i++)
            {
                if (a[i]==b[0])
                {
                    for(int j=0;j<b.length;j++)
                    {
                        while(flag==0)
                        {
                            if(b[j]==a[i])
                            {
                                flag=0;
                            }
                            else if(b[j] != a[i])
                            {
                                flag=1;
                            }
                            i++;
                        }
                    }
                }
            }
            if (flag==0)
            {
                System.out.println("Word Found !!!");

            }
            else 
            System.out.println("Word not Found");
        }

    }

P.S. : I know I can use the contains() function but I can as my professor suggests against it and could someone please correct the program I have written, because I could have scavenged off a program from the internet too if I had to, I just wanted to use my own logic

Thank You(again)

5 Answers5

0
while(flag==0)
{
  if(b[j]==a[i])
  {
    flag=0;
  }
  else if(b[j] != a[i])
  {
    flag=1;
  }
  i++;
  j++; //add this and try once 
}
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

If you are comparing strings in Java, you have to use equals();

So, stringA.equals(stringB);

Cheers!

obey
  • 796
  • 7
  • 16
0

You can just use String.contains();

If you really want to implement a method, try this one:

public static void main(String[] args) {

    // Initialize values searchedWord and original by user
    String original = [get original word from user] ;
    String searchedWord = [get searched for word from user];

    boolean containsWord = false;
    int comparePosition = 0;


        for(int i = 0; i < original.length() - searchedWord.length(); i++) {
            if(original.charAt(i) == searchedWord.charAt(comparePosition)) {
                comparePosition += 1;
            } else {
                comparePosition = 0;
            }
            if(comparePosition == searchedWord.length()) {
                containsWord = true;
                break;
            }
        }

    return containsWord? "Word found!!" : "Word not found.";
}
Kaschwenk
  • 522
  • 5
  • 16
0

Let me get this straight. You're looking for b array inside of a array? "Enter the string to be searched" means that you are searching the other way around, but I'll go with the logic your code seems to follow... Here's a naive way to do it:

            if (a[i]==b[0])
            {
                flag = 0;
                for(int j=0;j<b.length;j++)
                {
                    if(b[j] != a[i+j])  // will array index out of bounds when its not foud
                    {
                        flag++;  // you should probably break out of a named loop here
                    }

                }
                if(flag == 0){/*win*/}
            }

You're modifying your first search loop with variable i when you don't have to. You can just add i to j. Also, you don't need the while loop inside if i'm understanding your problem. Like others have said, functions exist to do this already. This algorithm isn't even as efficient as it could be.

I know of an algorithm where you check starting in the last character in b instead of the first character in b to begin with. Then you can use that information to move your search along faster. Without resorting to full pseudo code, anyone know what that's called?

Carlos Bribiescas
  • 4,197
  • 9
  • 35
  • 66
0

The simple way(but not the fastest way) is use double loop to check the chars in strings one by one, pls ref to my code and comments:

public class SearchString {

public static void main(String[] args) {
    String a = "1234567890";
    String b = "456";
    // Use toCharArray() instead of loop to get chars.
    search(a.toCharArray(), b.toCharArray());
}

public static void search(char[] a, char[] b) {
    if (a == null || b == null || a.length == 0 || b.length == 0) {
        System.out.println("Error: Empty Input!");
        return;
    }

    int lenA = a.length, lenB = b.length;

    if (lenA < lenB) {
        System.out
                .println("Error: search key word is larger than source string!");
        return;
    }

    // Begin to use double loop to search key word in source string
    for (int i = 0; i < lenA; i++) {
        if (lenA - i < lenB) { // If the remaining source string is shorter than key word.
                               // Means the key word is impossible to be found.
            System.out.println("Not found!");
            return;
        }
        // Check the char one by one.
        for (int j = 0; j < lenB; j++) {
            if (a[i + j] == b[j]) {
                if (j == lenB - 1) { // If this char is the last one of key word, means it's found!
                    System.out.println("Found!");
                    return;
                }
            } else {
                // If any char mismatch, then right shift 1 char in the source string and restart the search
                break;
            }
        }
    }

}

}
yellowB
  • 2,900
  • 1
  • 16
  • 19