英文:
Java duplicate character in string issue
问题
public static String duplicate(String s, String letter){
String rv = s;
for(int i=0; i<s.length(); i++){
String a = s.substring(i,i+1);
if (letter.compareTo(a) == 0){
rv = rv.substring(0, i) + a + rv.substring(i);
}
}
return rv;
}
英文:
I have an assignment for school in which I need to make a function that takes in a string and a letter. It then duplicates the letter in the string and returns it. For example, if I call the function like this
System.out.println(duplicate("hello world", "l"));
, it should output hellllo worlld. When I do make this call however, the code instead outputs hellllo wlorld. Here is my code:
public static String duplicate(String s, String letter){
String rv = s;
for(int i=0; i<s.length(); i++){
String a = s.substring(i,i+1);
if (letter.compareTo(a) == 0){
rv = rv.substring(0, i) + a + rv.substring(i);
}
}
return rv;
}
答案1
得分: 1
如果您可以使用indexOf,就不需要在for循环中遍历所有字符。
int i = 0;
while (true) {
int index = s.indexOf(letter, i);
if(index == -1 ) return s;
s = s.substring(0, index) + letter + s.substring(index);
i = index + 2;
}
英文:
if you can use indexOf, you don't need to iterate through all the characters in the for loop.
int i = 0;
while (true) {
int index = s.indexOf(letter, i);
if(index == -1 ) return s;
s = s.substring(0, index) + letter + s.substring(index);
i = index + 2;
}
答案2
得分: 1
好的,以下是您提供的内容的翻译:
所以,一个非常快速的桌面检查将突显潜在的问题...
+----+---+----+
| i | a | rv |
+----+---+----+
| 00 | h | h |
| 01 | e | e |
| 02 | l | l |
| 03 | l | l |
| 04 | o | l |
| 05 | | l |
| 06 | w | o |
| 07 | o | |
| 08 | r | w |
| 09 | l | o |
| 10 | d | o |
+----+---+----+
问题是,“插入”点没有被正确更新,而是没有偏移地继续使用当前的搜索索引。
我的个人偏好是使用 StringBuilder
,但由于您不能这样做,一个极其简单的方法仍然会遵循相同的一般方法,可能如下所示...
public static String duplicate(String s, String letter) {
String rv = "";
for (int i = 0; i < s.length(); i++) {
String a = s.substring(i, i + 1);
if (letter.compareTo(a) == 0) {
rv += a;
}
rv += a;
}
System.out.println(rv);
return rv;
}
但是,如果您不被允许这样做,那么您需要根据在它之前插入的附加字符数来跟踪每个新字符所需的“偏移”,例如...
public static String duplicate(String s, String letter) {
String rv = s;
int delta = 0;
for (int i = 0; i < s.length(); i++) {
String a = s.substring(i, i + 1);
if (letter.compareTo(a) == 0) {
rv = rv.substring(0, i + delta) + a + rv.substring(i + delta);
delta += 1;
}
}
System.out.println(rv);
return rv;
}
英文:
So, a really quick desk check will highlight the underlying issue...
+----+---+----+
| i | a | rv |
+----+---+----+
| 00 | h | h |
| 01 | e | e |
| 02 | l | l |
| 03 | l | l |
| 04 | o | l |
| 05 | | l |
| 06 | w | o |
| 07 | o | |
| 08 | r | w |
| 09 | l | o |
| 10 | d | o |
+----+---+----+
The problem is, the "insert" point is not been updated correctly, instead of offsetting the location by the number of changes that have already been made, you're simply continuing to use the current search index.
My personal preference would be to use a StringBuilder
, but since you can't do that, a ultra simplistic method which would follow the same general approach anyway, might look something like...
public static String duplicate(String s, String letter) {
String rv = "";
for (int i = 0; i < s.length(); i++) {
String a = s.substring(i, i + 1);
if (letter.compareTo(a) == 0) {
rv += a;
}
rv += a;
}
System.out.println(rv);
return rv;
}
But, if you're not allowed to do that, then you would need to keep track of the "offset" that each new character would need, based on the number of additional characters inserted before it, for example...
public static String duplicate(String s, String letter) {
String rv = s;
int delta = 0;
for (int i = 0; i < s.length(); i++) {
String a = s.substring(i, i + 1);
if (letter.compareTo(a) == 0) {
rv = rv.substring(0, i + delta) + a + rv.substring(i + delta);
delta += 1;
}
}
System.out.println(rv);
return rv;
}
答案3
得分: 0
考虑到所有的约束条件,这是我能想出的方案。
public static String duplicate(String s, String letter){
String ans = "";
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, i + 1);
if(sub.compareTo(letter) == 0)
ans += sub + sub;
else
ans += sub;
}
return ans;
}
如评论部分建议的,使用StringBuilder是首选方式。
public static String duplicate(String s, String letter){
StringBuilder ans = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, i + 1);
if(sub.compareTo(letter) == 0)
ans.append(sub + sub);
else
ans.append(sub);
}
return ans.toString();
}
英文:
Considering all your constraints, this is what I could come up with.
public static String duplicate(String s, String letter){
String ans = "";
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, i + 1);
if(sub.compareTo(letter) == 0)
ans += sub + sub;
else
ans += sub;
}
return ans;
}
As suggested in the comment section, StringBuilder is the preferred way
public static String duplicate(String s, String letter){
StringBuilder ans = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, i + 1);
if(sub.compareTo(letter) == 0)
ans.append(sub + sub);
else
ans.append(sub);
}
return ans.toString();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论