1.给出一句英文句子: "let there be light"
得到一个新的字符串,每个单词的首字母都转换为大写
知识点:split(),substring(start,end),toUpperCase(),length()
1 public class test { 2 public static void main(String[] args) { 3 String s = "let there be light"; 4 String[] s1 = s.split(" "); 5 s = ""; 6 for (String x : s1) { 7 x = x.substring(0, 1).toUpperCase() + x.substring(1, x.length()) + " "; 8 s += x; 9 } 10 s=s.trim(); 11 System.out.println(s); 12 } 13 }
2.英文绕口令
peter piper picked a peck of pickled peppers
统计这段绕口令有多少个以p开头的单词
知识点:charAt(),spilt()
1 public class test { 2 public static void main(String[] args) { 3 String s = "peter piper picked a peck of pickled peppers"; 4 int sum=0; 5 for(String x:s.split(" ")) 6 { 7 if(x.charAt(0)=='p') 8 sum++; 9 } 10 System.out.printf("一共%d个",sum); 11 } 12 }
3.间隔大小写模式
把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
知识点:toCharArray(),String.valueOf(),toUpperCase()
1 public static void main(String[] args) { 2 String s = "lengendary"; 3 char []cs=s.toCharArray(); 4 s=""; 5 for(int i=0;i<cs.length;i++){ 6 if((i+1)%2!=0){ 7 s+=String.valueOf(cs[i]).toUpperCase();//char转String可以用String.valueOf(char) 8 } 9 else{ 10 s+=cs[i]; 11 } 12 } 13 System.out.println(s); 14 }
4.把 lengendary 最后一个字母变大写
1 public static void main(String[] args) { 2 String s = "lengendary"; 3 s=s.substring(0,s.length()-1)+s.substring(s.length()-1,s.length()).toUpperCase(); 4 System.out.println(s); 5 6 }
5.练习-把最后一个two单词首字母大写
Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
1 public static void main(String[] args) { 2 String s = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak"; 3 int last_index_of_Two = s.lastIndexOf("two"); 4 5 String s1 = s.substring(0, last_index_of_Two); 6 String keyWord = s.substring(last_index_of_Two, last_index_of_Two + 1).toUpperCase(); 7 String s2 = s.substring(last_index_of_Two + 1, s.length()); 8 9 s = s1 + keyWord + s2; 10 System.out.println(s); 11 }
6.比较字符串
PS:这题有点意思~
创建一个长度是100的字符串数组
使用长度是2的随机字符填充该字符串数组
统计这个字符串数组里重复的字符串有多少种
1 package property; 2 3 public class test { 4 public static String randString(int n) {// 生成n位随机字符串 5 String s = ""; 6 for (; n > 0; n--) { 7 while (true) { 8 char c = (char) Math.round(Math.random() * 126); 9 if (Character.isLetter(c) || Character.isDigit(c)) { 10 s += c; 11 break; 12 } 13 } 14 } 15 return s; 16 } 17 18 public static void print(String s[])// 每行打印十个 19 { 20 int i; 21 for (i = 0; i < s.length; i++) { 22 if ((i + 1) % 10 != 0) { 23 System.out.printf(s[i] + " "); 24 } else { 25 System.out.printf("%s ", s[i]); 26 } 27 } 28 } 29 30 public static void func(String s[]) { 31 String repS = ""; 32 int sum = 0; 33 int judge[] = new int[100];// 辅助数组judge[],标记重复过的字符串,judge[i]=1则遍历时跳过i 34 for (int i = 0; i < judge.length; i++) {// 初始化judge[] 35 judge[i] = 0; 36 } 37 int i, j, k;// i,j用来遍历,k用来记录最后一个相同的字符串的位置 38 for (i = 0; i < s.length && judge[i] == 0; i++) { 39 // judge[i] = 1;//可写可不写 40 int flag = 0;// 标记内层循环是否找到了相同的字符串,1:找到了 41 k = i; 42 for (j = i + 1; j < s.length && judge[j] == 0; j++) { 43 44 if (s[i].equals(s[j])) { 45 judge[j] = 1; 46 flag = 1; 47 k = j; 48 } 49 } 50 if (flag == 1) { 51 sum++; 52 repS += " " + s[k]; 53 } 54 } 55 System.out.printf("一共有%d种重复字符串 ", sum); 56 if (sum != 0) 57 System.out.println("它们是:" + repS.trim()); 58 } 59 60 public static void main(String[] args) { 61 62 String s[] = new String[100]; 63 for (int i = 0; i < s.length; i++) { 64 s[i] = randString(2); 65 } 66 print(s); 67 func(s); 68 69 } 70 }
运行结果: