Compare 2 Strings | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Compare 2 Strings

I know how to compare 2 Strings, but I want to write out to the console parts of Strings that are same for both. Example: String text1 = "random"; String text2 = "tworanthing"; So here "ran" is same for both Strings. I want to know how to output it.

26th Jan 2018, 6:27 PM
Sad
Sad - avatar
7 Answers
+ 7
A way is to use two loops, an outer loop and inner loop. The outer loop loops through all possible substrings of the first string and the inner will loop through all substrings of the second string(with length equal to the length of the current substring of the first string) to find all possible "equal" substrings
26th Jan 2018, 6:48 PM
David Akhihiero
David Akhihiero - avatar
+ 6
What attempt have you made to do this?
26th Jan 2018, 6:34 PM
David Akhihiero
David Akhihiero - avatar
3rd Feb 2018, 1:39 PM
David Akhihiero
David Akhihiero - avatar
+ 5
@RandomCoder if you need further help, drop a comment on my code as I may miss a notification from here
27th Jan 2018, 8:41 PM
David Akhihiero
David Akhihiero - avatar
+ 2
Thanks, I got it. I will give it a try :)
26th Jan 2018, 6:53 PM
Sad
Sad - avatar
+ 1
I did not make it out because I don't know how to do it :P I tried to compare two Strings but it returns true or false so it is useless. I want to write same parts of those strings and not just return true of false
26th Jan 2018, 6:40 PM
Sad
Sad - avatar
+ 1
String text1 = "aabbccdd"; String text2 = "eehhkkcc"; So same here is "cc" and I want to write it
26th Jan 2018, 6:41 PM
Sad
Sad - avatar