练习一每个单词的首字母都转换为大写
题目描述:给出一句英文句子: “let there be light”
得到一个新的字符串,每个单词的首字母都转换为大写
String sentence = "let there be light";
char[] arr = sentence.toCharArray();
arr[0] = (char)((int)arr[0]-32);
for(int i=0;i< arr.length;i++) {
if(arr[i] == ' ')
arr[i+1] = (char)((int)arr[i+1]-32);
}
System.out.println(arr);
练习二 统计句子中某字母出现的次数
英文绕口令
题目描述:peter piper picked a peck of pickled peppers
统计这段绕口令有多少个以p开头的单词
//练习二英文绕口令
String sentence1 = "peter piper picked a peck of pickled peppers";
int sum = 0;
for(int i=0;i<sentence1.length();i++) {
if(sentence1.charAt(i) == 'p')
sum++;
}
System.out.println(sum);
练习三
题目描述:把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
//练习三间隔大写小写模式
String sentense2 = "lengendary";
char[] arr1 = sentense2.toCharArray();
for(int i = 0;i< arr1.length;i++) {
if((i & 1) == 0)
arr1[i] = (char)((int)arr1[i]-32);
}
System.out.println(arr1);
练习四
题目描述: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
把最后一个two单词首字母大写
String sentence3 = "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";
int index = sentence3.lastIndexOf("two");
System.out.println(index);
char[] arr3 = sentence3.toCharArray();
arr3[index] = (char)((int)arr3[index]-32);
System.out.println(arr3);