gpt4 book ai didi

java - 如何在不使用 Java 中的 Replace() 的情况下替换字符串中的字符?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:42:31 25 4
gpt4 key购买 nike

我在做这个作业时遇到了问题:

Given a string, replace the first occurrence of 'a' with "x", the second occurrence of 'a' with "xx" and the third occurrence of 'a' with "xxx". After the third occurrence, begin the replacement pattern over again with "x", "xx", "xxx"...etc.; however, if an 'a' is followed by more than 2 other 'a' characters in a row, then do not replace any more 'a' characters after that 'a'.

No use of the replace method is allowed.

aTo123X("ababba") → "xbxxbbxxx"

aTo123X("anaceeacdabnanbag") → "xnxxceexxxcdxbnxxnbxxxg"

aTo123X("aabaaaavfaajaaj") → "xxxbxxxaaavfaajaaj"

aTo123X("pakaaajaaaamnbaa") → "pxkxxxxxxjxxaaamnbaa"

aTo123X("aaaak") → "xaaak"

我的代码输出包含 a,添加了 x,但 x 的数量不正确。


public String aTo123X(String str) {
/*
Strategy:
get string length of the code, and create a for loop in order to find each individual part of the String chars.check for a values in string and take in pos of the a.
if one of the characters is a
replace with 1 x, however, there aren't more than 2 a's immediately following first a and as it keeps searching through the index, add more x's to the original string, but set x value back to 1 when x reaches 3.
if one of characters isn't a,
leave as is and continue string.
*/

String xVal = "";
String x = "x";
String output = "";
for (int i = 0; i < str.length(); i++){

if( str.charAt(i) == 'a'){
output += x;
str.substring(i+1, str.length());
}
output += str.charAt(i);
}
return output;
}

最佳答案

这是执行相同操作的代码。我已经评论了代码以解释它的作用

public class ReplaceChar {

public static void main(String... args){
String[] input =new String[]{"ababba","anaceeacdabnanbag","aabaaaavfaajaaj"};

StringBuilder result = new StringBuilder();

for (int i= 0; i < input.length;i++){
result.append(getReplacedA(input[i]));
result.append("\n");
}

System.out.println(result);

}

private static String getReplacedA(String withA){
// stringBuilder for result
StringBuilder replacedString = new StringBuilder();

// counting the number of time char 'a' occurred in String for replacement before row of 'aaa'
int charACount = 0;

// get the first index at which more than two 'aa' occurred in a row
int firstIndexOfAAA = withA.indexOf("aaa") + 1;

// if 'aaa' not occurred no need to add the rest substring
boolean addSubRequired = false;

// if the index is 0 continue till end
if (firstIndexOfAAA == 0)
firstIndexOfAAA = withA.length();
else
addSubRequired = true;

char[] charString = withA.toCharArray();

//Replace character String[] array
String[] replace = new String[]{"x","xx","xxx"};

for(int i = 0; i < firstIndexOfAAA; i++){
if (charString[i] == 'a'){
charACount++;
charACount = charACount > 3 ? 1 : charACount ;
// add the number x based on charCount
replacedString.append(replace[charACount - 1]);
}else{
replacedString.append(charString[i]);
}
}

// if the String 'aaa' has been found previously add the remaining subString
// after that index
if (addSubRequired)
replacedString.append(withA.substring(firstIndexOfAAA));

// return the result
return replacedString.toString();
}

}

输出:

xbxxbbxxx
xnxxceexxxcdxbnxxnbxxxg
xxxbxxxaaavfaajaaj

编辑:一些改进您可以在 getReplacedA() 函数中针对某些特殊情况进行改进:

  1. 检查 char 'a' 是否存在于字符串中,如果不存在则直接返回字符串。无需进一步执行任何操作。

  2. 使用 IgnoreCase 避免大写或小写的可能性。

关于java - 如何在不使用 Java 中的 Replace() 的情况下替换字符串中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com