Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Checking whether a string is a substring of another string without using indexOf

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Checking whether a string is a substring of another string without using indexOf

    The following topic has been cross posted here.

    Write a method that checks whether a string is a substring of another string. Write a test program that prompts the user to enter two strings, and check whether the first string is a substring of the second. Do not use indexOf or contains methods.

    Here is my most up to date version of my code:
    import java.util.*;
    import java.lang.String;
     
    public class CheckingSubstring2 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a String: ");
    		String string1 = input.next();
    		System.out.print("Please enter a second String: ");
    		String string2 = input.next();
    		if (isSubstring(string1, string2)) {
    			System.out.println("The first string is a substring of the second.");
    		} else {
    			System.out.println("The first string is NOT a substring of the second.");
    		}
    	}	
    	public static boolean isSubstring(String string1, String string2) {
    		char c;
    		char d;
    		boolean match = true;
    		for (int i = 0; i < string1.length(); i++) {
    			c = string1.charAt(i);
    			for (int j = 0; j < string2.length(); j++) {
    				d = string2.charAt(j);
    				if (d != c) {
    					match = false;
    					System.out.println("MATCH: N The value of c and d: " + c + " and " + d);	
    				} else {
    					System.out.println("MATCH: Y The value of c and d: " + c + " and " + d);
    				}
    				match = true;	
    			}
    		}
    		return match;
    	}
    }
    My program compiles but does not work correctly. I have chosen to cross post this problem because I still have no idea what is wrong with my code. Can someone please tell me the line numbers that are causing the problem to not work properly? Thanks.
    Last edited by m2msucks; December 8th, 2011 at 10:22 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Checking whether a string is a substring of another string without using indexOf

    After a quick look my suggestion is the inner for loop should only be executed if the current char of String1 is the same as the first char of String2. Then the inner loop should check each subsequent pair of chars and break back to the outer loop as soon as they do not match. If you get to the end of the inner loop then it must be a substring.
    Improving the world one idiot at a time!

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Checking whether a string is a substring of another string without using indexOf

    for (int j = 0; j < string2.length(); j++) {
    				d = string2.charAt(j);
    				if (d != c) {
    					match = false;
    					System.out.println("MATCH: N The value of c and d: " + c + " and " + d);	
    				} else {
    					System.out.println("MATCH: Y The value of c and d: " + c + " and " + d);
    				}
    				match = true;	
    			}
    Your match will always be true in this piece of code. Move match=true inside else. Also, you are comparing each character of first string to every character of second, you must break as soon as your match gets true and get the index of the string 2. Then iterate the outer loop and check for the inner loop character at index+1 returned. Keep on searching until you get the consecutive characters in the second string.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Checking whether a string is a substring of another string without using indexOf

    @Junky Thanks. I modified my code and it seems like it's working. I've tried using several different strings and they all seem to work. For some reason, I think there might still be something wrong with my code. Does it look okay?

    @Mr.777 I eventually realized that when I was working on my code some more so I made lots of changes. When you say "get the index of the string 2", do you mean use the indexOf method? I'm not allowed to use that method so I just used a counter instead. Does my code look okay?
    import java.util.*;
    import java.lang.String;
     
    public class CheckingSubstring2 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a String: ");
    		String string1 = input.next();
    		System.out.print("Please enter a second String: ");
    		String string2 = input.next();
    		if (isSubstring(string1, string2)) {
    			System.out.println("The first string is a substring of the second.");
    		} else {
    			System.out.println("The first string is NOT a substring of the second.");
    		}
    	}	
    	public static boolean isSubstring(String string1, String string2) {
    		char c;
    		char d;
    		boolean match = true;
    		int count = 1;
    		int counter = 0;
    		boolean matchFound = false;
    		for (int i = 0; i < string1.length(); i++) {
    			c = string1.charAt(i);
    			//Part 2: This part determines if the characters following the 1st matching character are also matches
    			if (matchFound == true) {
    				for (int j = count + counter; j < string2.length(); j++) {
    					d = string2.charAt(j);
    					if (d != c) {
    						System.out.println("MATCH2: N The value of c and d: " + c + " and " + d);	
    						match = false;
    					} else {
    						counter++;
    						System.out.println("MATCH2: Y The value of c and d: " + c + " and " + d + " " + counter);
    						matchFound = true;
    						break;
    					}
    				}
    			}	
    			//Part1: This part determines wheter the 1st character of string1 has a match in string2
    			//If a match is found, Part 2 happens
    			if (matchFound == false) {
    				for (int k = 0; k < string2.length(); k++) {
    					d = string2.charAt(k);	
    					if (d != c) {
    						count ++;
    						System.out.println("MATCH: N The value of c and d: " + c + " and " + d + " " + count);	
    						matchFound = false;
    					} else {
    						System.out.println("MATCH: Y The value of c and d: " + c + " and " + d + " " + count);
    						matchFound = true;
    						break;
    					}	
    				match = true;
    				}	
    			}	
    		}
    			return match;
    	}
    }
    Last edited by m2msucks; December 9th, 2011 at 02:27 AM.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Checking whether a string is a substring of another string without using indexOf

    When you say "get the index of the string 2", do you mean use the indexOf method?
    No, i don't mean use indexOf() as you can simply return the value of i or j variable depending upon the index you are comparing with and match=true.
    Does my code look okay?
    You can say 50%. Input it
    String1: sha
    String2: shaky
    You will get the correct answer but,
    String1: sha
    String2: shkshaky
    You will get the wrong.
    Well, you are almost close. Good luck.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Checking whether a string is a substring of another string without using indexOf

    Oh I see. I guess my code isn't working correctly. I also found out I need to put a test to make sure string2 is longer than string1. Thanks.
    Last edited by m2msucks; December 9th, 2011 at 12:50 PM.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Checking whether a string is a substring of another string without using indexOf

    import java.util.*;
    import java.lang.String;
     
    public class CheckingSubstring2 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a String: ");
    		String string1 = input.next();
    		System.out.print("Please enter a second String: ");
    		String string2 = input.next();
    		if (isSubstring(string1, string2)) {
    			System.out.println("The first string is a substring of the second.");
    		} else {
    			System.out.println("The first string is NOT a substring of the second.");
    		}
    	}	
    	public static boolean isSubstring(String string1, String string2) {
    		char c;
    		char d;
    		boolean match = true;
    		int count = 1;
    		int counter = 0;
    		boolean matchFound = false;
    		for (int i = 0; i < string1.length(); i++) {
    			c = string1.charAt(i);
    			if (string1.length() > string2.length()) {
    				match = false;
    				break;
    			}
    			//Part 2: This part determines if the characters following the 1st matching character are also matches
    			if (matchFound == true) {
    				for (int j = count + counter; j < string2.length(); j++) {
    					d = string2.charAt(j);
    					if (d != c) {
    						System.out.println("MATCH2: N The value of c and d: " + c + " and " + d);	
    						match = false;
    					} else {
    						counter++;
    						System.out.println("MATCH2: Y The value of c and d: " + c + " and " + d + " " + counter);
    						matchFound = true;
    						break;
    					}
    				}
    			}	
    			//Part1: This part determines wheter the 1st character of string1 has a match in string2
    			//If a match is found, Part 2 happens
    			if (matchFound == false) {
    				for (int k = 0; k < string2.length(); k++) {
    					d = string2.charAt(k);	
    					if (d != c) {
    						count ++;
    						System.out.println("MATCH: N The value of c and d: " + c + " and " + d + " " + count);	
    						matchFound = false;
    					} else {
    						System.out.println("MATCH: Y The value of c and d: " + c + " and " + d + " " + count);
    						matchFound = true;
    						break;
    					}	
    				match = true;
    				}	
    			}	
    		}
    			return match;
    	}
    }
    I'm going to turn in my code like this. It's not perfect but it is close.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Checking whether a string is a substring of another string without using indexOf

    Good Luck. So i hope, you've solved the problem.

Similar Threads

  1. Replies: 2
    Last Post: October 22nd, 2011, 01:34 AM
  2. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 09:33 AM
  3. Replies: 1
    Last Post: February 4th, 2010, 03:23 PM
  4. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM
  5. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 06:12 AM