Description
对于输入字符串s(假设字符串只包含字母构成的单词和空格),完成如下功能: 1. 统计该字符串中字母c出现的次数 2. 求该字符串的逆 3. 输出该字符串中子串str的所有位置(无需考虑子串叠加现象) 4. 将字符串中每个单词的第一个字母变成大写并输出
Input
字符串s 字母c 子串str
Output
c在s中出现的次数 s的逆 str在s中的所有位置 所有单词首字母大写后的字符串
Sample Input
I scream you scream we all scream for icecream m eam
Sample Output
4 maerceci rof maercs lla ew maercs uoy maercs I 5 16 30 43 I Scream You Scream We All Scream For Icecream
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
//输入字符
String sc = scan.nextLine();
char c = sc.charAt(0);
//字符串逆转
String reverse = "";
int num = 0;
int len = s.length();
for(int i = 0;i < len;i++)
{
reverse = s.charAt(i) + reverse;
if(s.charAt(i) == c)
{
num ++;
}
}
System.out.println(num);
System.out.println(reverse);
//子串
String ss = scan.nextLine();
int temp = 0;
temp = s.indexOf(ss);
String index = "";
index = index + Integer.toString(temp) ;
while(temp != s.lastIndexOf(ss))
{
temp = s.indexOf(ss, temp+1);
index = index +" " + Integer.toString(temp);
}
System.out.println(index);
//大小写转变
String space = " ";
String another = "";
for(int i = 0;i<len;i++)
{
another += s.charAt(i);
if(s.charAt(i) == ' ')
{
//another += s.charAt(i);
another += String.valueOf(s.charAt(i+1)).toUpperCase();
i++;
}
}
System.out.println(another);
}
}