Sunday, March 2, 2014

Compare two strings in java

import java.util.Scanner;
class reg2{
public static void main(String []arg){
String a2 = null;
String b2 = null;

Scanner sr = new Scanner(System.in);
System.out.print("Enter first string :: ");
a2 = sr.nextLine();
System.out.print("Enter second string :: ");
b2 = sr.nextLine();

if(scheck(a2,b2)==1)
System.out.println("strings \""+a2+"\" and \""+b2+"\" were matched");
else System.out.println("sorry , strings \""+a2+"\" and \""+b2+"\" were unmatched");
}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}




OUTPUT







No comments: