2 string using recursion

Do you have a compilation error or a difficult problem to solve? Ask for help here
Post Reply
maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

2 string using recursion

Post by maty.tsoraro@gmail.com » Fri Feb 09, 2018 12:49 am

i am trying to write a cose that gets two strings s1 ,s2.
return true if s2 is a substring of s1

this is the method :

public static boolean isSubstring (String s1, String s2)
{
//2 stop condition
if(s1.length() == 0)
return false;
if(s2.length() == 0)
return true;
//in case the first characters in both strings are equal
if(s1.charAt(0) == s2.charAt(0))
if(isPrefix(s1.substring(1), s2.substring(1)))
return true;
return isSubstring(s1.substring(1),s2);
}

//this is the method that inside the method
public static boolean isSubstring (String s1, String s2)
{
//2 stop condition
if(s1.length() == 0)
return false;
if(s2.length() == 0)
return true;
//in case the first characters in both strings are equal
if(s1.charAt(0) == s2.charAt(0))
if(isPrefix(s1.substring(1), s2.substring(1)))
return true;
return isSubstring(s1.substring(1),s2);
}

this is the main :

public class Substring{
public static void main(String[] args){
isSubstring(abcde,abc);
}
}

getting alot of error from this sort :
Compilation Errors Detected

File: USER_155552/source/isPrefix.java
Line: 2
class, interface, or enum expected

please help!!

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: 2 string using recursion

Post by dbremmen@gmail.com » Sat Feb 10, 2018 8:40 pm

Hi!

The problem is that you had methods not surrounded by classes inside the files. I accessed your program and changed it so now each method is in its own class. Tell me if that fixes the problem

Regards!
David

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: 2 string using recursion

Post by maty.tsoraro@gmail.com » Sat Feb 10, 2018 10:34 pm

yes it worked !
thanks alot David :P

Post Reply