给定两个字符串,求解给定字符串的后缀。按照如下的格式进行输出
例如:
输入:
father mather
输出:
The common suffix is ather.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
int count = 0;
int i = str1.length() - 1;
for(int j = str2.length() - 1;j >= 0;j--) {
if(ch1[i] == ch2[j]) {
count++;
i--;
}else {
break;
}
}
if(count == 0)
System.out.println("No common suffix.");
else {
System.out.print("The common suffix is ");
for(i = str1.length() - count;i < str1.length();i++) {
System.out.print(ch1[i]);
}
System.out.println(".");
}
}