package homework;
import java.util.Scanner;
/**
* 编程题:
1: 字符串反转
举例:键盘录入”abc”
输出结果:”cba”
*
*/
public class BianChengTi1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String s = sc.nextLine();
StringBuffer sb = new StringBuffer(s) ;
String str = sb.reverse().toString();
System.out.println("结果:"+str);
}
}
package homework;
import java.util.Scanner;
/**
* 2:统计大串中小串出现的次数
举例:
在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
结果:
java出现了5次
*
*/
public class BianChengTi2 {
public static void main(String[] args) {
String str1 ="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要查找的字符串");
String str2 = sc.nextLine();
int sum = sum(str1,str2);
System.out.println("该字符串出现的次数为:"+sum);
}
public static int sum(String s1,String s2) {
int sum = 0;
int index = s1.indexOf(s2);
while(index!=-1) {
sum++;
int start = index + s2.length();
s1 = s1.substring(start);
index = s1.indexOf(s2);
}
return sum;
}
}