+3

What's wrong with this code ?

https://code.sololearn.com/cKWyEiup55o4/?ref=app

10/19/2019 6:47:10 PM

Max

25 Answers

New Answer

-1

import java.util.Scanner; public class naive { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1,s2; int flag = 0; System.out.println("enter the string"); s1 = sc.nextLine(); System.out.println(s1); System.out.println("enter the pattren"); s2 = sc.nextLine(); System.out.println(s2); int m = s1.length(); int n = s2.length(); if(m<=0 || n<=0 || n>m) flag = 0; else { for(int i=0;i<=m-n;i++) { if((s2.compareTo(s1.substring(i,i+n)))==0) { flag = 1; break; } else { flag =0; } } } if(flag == 0) System.out.println("Not found"); else if(flag == 1) System.out.println("Found"); } }

+4

Max but you used contains method then how it is naive?

+3

Thanks Kilowac works fine. 😊

+3

Max I think you need this. I changed code and created a method which will return Boolean value. https://code.sololearn.com/c6kZN8kGkVVf/?ref=app

+2

Use s1.contains(s2), its a boolean. you can just use one if else with the contains and you wont have to use flag

+1

MAYANK SINGH NIKUMBH I'm not sure exactly why, but an empty string means nothing and "a" technically has nothing next to it so it's true. I don't know why, probably how scanner reads inputs.

+1

AJ || ANANT || AC || ANANY || AY Yup you are right. I guess contains is not right for naive. I guess it should be any string matching method or char array comparison.

+1

This code is shorter and it works https://code.sololearn.com/clc7tiE27n0A/?ref=app

0

A sample input can be :- abab and acab it gives result found. But as we can see acab is not present in abab string.

0

What was the error, i didn't run into any

0

Kilowac try the sample input one

0

Like this https://code.sololearn.com/c3imlf2SONFC/?ref=app

0

Kilowac when the s1 = a and s2 = blank .. then also it's found. "Like text is there but not pattern to be found in text is entered". This condition is also showing Found in your code. try input :- a 👈 blank line the Result is found.

0

Kilowac Yup exactly string has null at it's last. That's why it's showing found. But that should not happen in reality. That's what I set the flags for.

0

Ahh i c

0

Max Why this is naive String match. It makes confusion.

0

AJ || ANANT || AC || ANANY || AY It's naive because it just scrolls the pattern to be searched over the text in which it has to be searched. (Suppose like diagram on glossy paper sliding over main pic to match and complete the rest of diagram). There are 3 others which I know (algos) which are used for string matching :- 1) Rabin-Karp Algo 2) Finite Automata 3) KMP Algo These three preprocess the pattern string. And then search for the instances in text. This reduces the time complexity of the search. Hope this helps . 😊

0

Tamar Peer for(int i=0;i<=m-n;i++) { if(s1.contains(s2)) { flag = true; break; } } What is the use of loop here? It can be like this also if(s1.contains(s2)) { System.out.println("found"); } else { System.out.println("Not Found"); } And also this is not NSM. It's a simple SubString checking.

0

Tamar Peer It should not compare strings using contains method... Or else it would not be naive ... It should compare via string comparing methods or char array[ ]. I have changed my code... Help in that plz. •Also your code throws exception when either of s1 or s2 string is left blank. • Try input :- hey how are you ? e y it gives not found... even though it is present.

0

It's a max program not naive