1、如下字符串,01#张三#20-02#李四#30-03#王五#40。。。。。。,解析每个人分数多少。样式如下:
01 张三 20
02 李四 30
03 王五 40。并且计算总分。
package Day0922;
import java.util.Arrays;
public class homework01 {
public static void main(String[] args) {
String str = "01#张三#20-02#李四#30-03#王五#40";
// 分割
String[] arr = str.split("-");
int sum = 0;
for (String value : arr) {
String[] s = value.split("#");
sum += Integer.parseInt(s[2]);
System.out.println(Arrays.toString(s));
}
System.out.println(sum);
}
}
2、输入一个字符串,将字符串中的所有敏感词全部替换为**
敏感词:色情,马勒戈壁,暴力,操,日。
package Day0922;
import java.util.Scanner;
public class homework02 {
public static void main(String[] args) {
String[] mgc = {"马勒戈壁", "日", "操"};
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
for (String s : mgc) {
str.replace(s, "**");
}
System.out.println(str);
}
}
3、将字符串“我爱中华人民共和国”,变成一个倒叙的字符串
package Day0922;
public class homework03 {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("我爱中华人民共和国");
sb1.reverse();
String sb2 = sb1.toString();
System.out.println(sb2);
}
}
4、最长公共前缀
创建一个字符串数组,求所有元素的最长公共前缀。
没有返回"“。
比如:
strs = [“flow”,“flower”,“flight”]
返回:“fl”
strs=[“dog”,“cat”,“fish”]
返回:”"
package Day0922;
public class homework05 {
public static void main(String[] args) {
String[] strs = {"flow", "flower", "flight"};
String prefix = "";
for (int i = 0; i < strs[0].length(); i++) {
prefix = strs[0].substring(0, i+1);
boolean flag = true;
for (String s : strs) {
if (!s.startsWith(prefix)) {
flag = false;
break;
}
}
if (flag == false) {
break;
}
}
System.out.println(prefix.substring(0,prefix.length()-1));
}
}
5、输入一段英文,求最后一个单词的长度
package Day0922;
import java.util.Scanner;
public class homework06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int index = s.lastIndexOf(" ") + 1;
System.out.println(s.length() - index);
}
}
6、输入一段话,将每个单词反转
package Day0922;
import java.util.Arrays;
import java.util.Scanner;
public class homework07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] strings = s.split(" ");
for (int i = 0; i < strings.length; i++) {
String str = strings[i];
StringBuffer sb = new StringBuffer(str);
strings[i] = sb.reverse().toString();
}
String s2 = Arrays.toString(strings);
System.out.println(s2);
}
}