【问题描述】
编写一个函数strrindex(s,t),用于返回字符串t在字符串s中最右边出现的位置.该位置从0开始计数,如果s中不含有t,那么返回-1;在你编写的程序中,使用strrindex(s,t)函数,输入t,s,输出t在s最右边的位置.
【输入形式】
控制台分行输入字符串s,t.
【输出形式】
控制台输出一个整数,是t在s最右边出现的位置.
【样例输入】
【样例输入】The strdup() function new returns a pointer to a new string
new
【样例输出】49
【样例说明】
输入的第一行为字符串s,第二行为字符串t=“new”.t在s中出现过两次,其中在最右边出现的位置中"new"的第一个字符’n’在s中所在的位置为49.
【评分标准】
该题要求输出一个整数,答对得满分10分,否则得0分。
【代码】
import java.util.Scanner;
public class ab
{
public static int strrindex(String s,String t)
{
int n1=s.length();
int n2=t.length();
int pos;
//boolean flag=false;
//char[] s1=s.toCharArray();
//char[] t1=t.toCharArray();
pos=s.indexOf(t);
return pos;
}
public static void main(String []args)
{
String s;
String t;
Scanner a = new Scanner(System.in);
s=a.nextLine();
t=a.nextLine();
a.close();
s=new StringBuffer(s).reverse().toString();
t=new StringBuffer(t).reverse().toString();
int ans=strrindex(s,t);
ans=s.length()-ans-t.length();
System.out.print(ans);
}
}