java - 大写转换——将字符串中每个单词的首字母转换为大写
问题描述
一些测试用例不起作用。我可以知道我哪里错了。测试用例“我喜欢编程正在工作,但 idk 的其他测试用例不工作。
class Solution
{
public String transform(String s)
{
// code here
char ch;
// String s = "i love programming";
String res="";
ch = s.charAt(0);
ch = Character.toUpperCase(s.charAt(0));
res +=ch;
for(int i=1;i<s.length();i++){
if(s.charAt(i) == ' '){
//System.out.println(i);
ch = Character.toUpperCase(s.charAt(i+1));
res+=' ';
res+=ch;
i++;
}else {
res+=s.charAt(i);
}
}
return res;
}
}
//Some test cases are not working. May I know where I went Wrong?
解决方案
这个解决方案对我来说非常有效,所有测试用例都通过了。谢谢你。
class Solution
{
public String transform(String s)
{
// code here
char ch;
// String s = "i love programming";
String res="";
ch = s.charAt(0);
ch = Character.toUpperCase(s.charAt(0));
res +=ch;
for(int i=1;i<s.length();i++){
if(s.charAt(i-1) == ' '){
//System.out.println(i);
ch = Character.toUpperCase(s.charAt(i));
res+=ch;
}else {
res+=s.charAt(i);
}
}
return res;
}
}
推荐阅读
- html - 如何在 Flask 中使用 jinja2 模板将用户输入值传递给图像 src?
- node.js - 如何在 SoapUI 中发布表单数据以测试 multer node.js 应用程序
- flutter - 离开颤振应用程序时如何防止出现闪屏?
- tsql - T-SQL - WHERE @Parameter LIKE 列+'%'
- python - top-5 的准确度是否可能低于 top-1 的准确度?
- mysql - MySQL - 如何删除重复的数据输入?
- postgresql - 插入时,将 NULL 替换为之前的非空值
- bootstrap-4 - 无法在引导表数据单元格悬停时查看工具提示信息
- mysql - 使用 Where Cluase 连接两个 mysql 表
- git - 如何使用 Lerna 在具有独立版本控制的 Mono repo 项目中创建测试分支和发布分支