关于字符串字母大小写转换的一些小练习

练习一每个单词的首字母都转换为大写

题目描述:给出一句英文句子: “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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值