0

我正在尝试编写代码以在句点“。”之后将第一个字母大写。

例如:

String str = "abcdef. ghijk, mlnopqrs. tuv .................." 
//the "............" means the line is still going on.

输出应该是:

"Abcdef. Ghijk, mlnopqrs. Tuv ................."

注意:只使用基本方法

字符串:charAt, indexOf, length, substring,toUpperCase

这意味着没有Stringbuilder,没有数组,没有拆分......等等。我在这个网站上找到的其他解决方案涉及这些事情。


编辑:非常感谢大家的快速回复。刚刚看到 Robin Krahl 的编辑。是的,我忘了添加我的代码。我的代码看起来与mau的回复完全一样。但是这段代码最终会出现这个异常

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围

从这里我不知道如何解决这个问题-.-

4

4 回答 4

1

我可以帮助你处理逻辑部分:

  1. 找到子字符串直到“。”
  2. 将 charAt 子字符串的第零位设为大写。
  3. 循环 1 到 2 直到不再有“.”。存在于字符串中
  4. 连接子串

希望这可以帮助

于 2013-10-24T23:27:03.643 回答
1
int length = str.length()
for(int x = 0; x < length-1; x++) {
  if(str.charAt(x) == '.') {
    str = str.substring(0,x+1) + (""+str.charAt(x+1)).toUpperCase() + str.substring(x+2,length+1);
  }
}

如果字母不是直接在句点之后,要么向前看两个,要么循环寻找下一个字母。你可以说

letter = str.charAt(x);
if((letter >= 'A' && letter <= 'Z") || (letter >= 'a') && letter <= 'z'))
于 2013-10-24T23:28:35.197 回答
1

我会这样做的方式:

  • 获取字符数组,使用String.toCharArray().
  • 使用计数器依次查看数组中的每个字符。
  • 当你找到一个句号时,设置一个标志。
  • 当您在设置标志时找到一个字母时,清除该标志并将该字符替换为其大写版本。
  • 一旦你完成了循环,创建一个新的字符串String.valueOf()
于 2013-10-24T23:29:06.627 回答
0

构建具有 2 个状态的状态机并让它扫描字符。

从状态 1 开始:如果输入是句点,则切换到状态 2

状态 2:如果输入不是句点而不是空格,则切换到状态 1

状态1:输出输入而不做修改

状态2:如果输入是一个字符,输出使用大写toUpper

import java.util.Scanner;
public class Capitalize {
  public static void main (String [] args) {
    Capitalize instance = new Capitalize();
    try (Scanner s = new Scanner(System.in)) {
      while (s.hasNext()) {
        instance.capitalize_and_print(s.nextLine());
      }
    }
  }

  public void capitalize_and_print (String s) {
    int state = 0;
    for (int i = 0; i < s.length(); ++i) {
      switch (state) {
        case 0:
          System.out.print(s.charAt(i));
          if (s.charAt(i) == '.') {
            state = 1;
          }
          break;
        case 1:
          System.out.print(Character.toUpperCase(s.charAt(i)));
          if (s.charAt(i) != '.' && s.charAt(i) != ' ') {
            state = 0;
          }
          break;
      }
    }
  }
}

更新:更简单的版本

public class Capitalize {
  public static void main (String [] args) throws java.io.IOException {
    int state = 2;
    int c = System.in.read();
    while (c != -1) {
      if (state == 1) {
        System.out.print((char)c);
        if (c == '.') {
          state = 2;
        }
      } else {
        System.out.print(Character.toUpperCase((char)c));
        if (c != '.' && c != ' ') {
          state = 1;
        }
      }
      c = System.in.read();
    }
  }
}

更新 2:更简单的版本

public class Capitalize {
  public static void main (String [] args) throws java.io.IOException {
    String str = "abcdef. ghijk, mlnopqrs. tuv ..................";
    int state = 2;
    for (int i = 0; i < str.length(); ++i) {
      char c = str.charAt(i);
      if (state == 1) {
        System.out.print(c);
        if (c == '.') {
          state = 2;
        }
      } else {
        System.out.print(Character.toUpperCase(c));
        if (c != '.' && c != ' ') {
          state = 1;
        }
      }
    }
  }
}
于 2013-10-24T23:30:58.310 回答