文章目录
1. 字符串处理 (10 分)
编写一个程序,用户输入任意一个字符串,显示它的长度和第一个字符。
输入格式:
输入任意一个字符串。
输出格式:
显示它的长度和第一个字符,其间用,分隔。
输入样例:
abc 4567
输出样例:
8,a
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String a= sc.nextLine();
int num= a.length();
System.out.println(num+","+a.charAt(0));
}
}
2.单词替换 (20 分)
设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
输入格式:
首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
输出格式:
完成查找替换后的完整字符串
输入样例:
在这里给出一组输入。例如:
Although I am without you, I will always be ou you
ou
with
输出样例:
在这里给出相应的输出。例如:
Although I am without you, I will always be with you
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
String d = a.replace(" "+b+" "," "+c+" ");
d = a.replace(" "+b," "+c);
d = a.replace(b+" ",c+" ");
System.out.println(d);
}
}
3.字符串处理 (10 分)
给定一个字符串。请去除串中的数字并反转。
输入格式:
原始串。
输出格式:
去除数字后的反转字符串。
输入样例:
he11ll00o w0or8ld!
输出样例:
!dlrow olleh
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = a.replaceAll("\\d", "");
System.out.println(b = new StringBuffer(b).reverse().toString());
}
}
4.通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 (10 分)
统计一行字符串中的英文字母个数、空格个数、数字个数、其他字符个数
输入格式:
通过键盘输入一行字符(任意字符)
输出格式:
统计一行字符串中的中英文字母个数、空格个数、数字个数、其他字符个数
输入样例:
rwrwewre234

&spm=1001.2101.3001.5002&articleId=117448651&d=1&t=3&u=f5670adfe6594e53b4db23912d6ae7d8)
5290

被折叠的 条评论
为什么被折叠?



