Java——字符串

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);

}

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值