Amazon Interview Question

Write a function to find a substring in a string. Test your code, write the test cases.

Interview Answers

Anonymous

May 13, 2017

def substrng(str1,str2): if str2 in str1: print ("substring found") else: print ("No substring found")

Anonymous

Mar 22, 2019

static boolean findSubString(String str) { String abc = "SaurabhThawali"; for(int i =0 ; i < str.length(); i++) { if( ! abc.contains(str)) { System.out.println("Not Found "); return false; } } System.out.println(" Found "); return true; }

Anonymous

Apr 3, 2017

public static void main(String[] args) { String str = "HelloWorld Vivek"; String ptr = ""; for ( int i = 0 ; i < str.length(); i++ ) { for ( int j = i+1; j <= str.length() ; j++) { ptr = str.substring(i,j); System.out.println(ptr); } } }

Anonymous

Jun 23, 2017

private static void substringsOfAString(String str){ for(int i=0; i<=str.length()-1; i++){ for(int j=i+1; j<=str.length();j++){ System.out.println(str.substring(i, j) ); } } }