How do I change 2+ values in a char array?

SchleptOn27

I turned a String into a char array and I was wondering if there was a way to replace 2 or more values in an array?

I'm working on a String guessing game that's a homework assignment, the User enters a sentence or word, then the user enters a letter, if the letter is part of the input, then the guessString updates, and replaces its '?' with the letter the user guessed.

For example:

User enters: racer

Original String: racer
Guess String:?????
Please guess a character

User enters: a

Original String: racer
Guess String:?a???
String input = "This is a string";
input = input.toLowerCase();
int inputLength = input.length();
System.out.println("You entered: " + input
            + "\n" + "The string length is: " + inputLength);
String answerString = "?".repeat(inputLength);
int answerStringLength = answerString.length();
System.out.println(answerString);
System.out.println(answerStringLength);

System.out.println("Guess a letter");
String guess = scan.nextLine();
System.out.println(guess);
int guessLength = guess.length();
System.out.println(guessLength);
while (guessLength != 1) {
    guess = scan.nextLine();
    guessLength = guess.length();
    System.out.println(guessLength);
}
System.out.println(guess);
String guessRepeat = guess.repeat(3);
char guessChar=guessRepeat.charAt(2);
System.out.println(guessChar);

int checkGuess = input.indexOf(guess);

char[] answerStringChars = answerString.toCharArray();
answerStringChars[checkGuess]=guessChar;
String newAnswerString = String.valueOf(answerStringChars);
System.out.println(newAnswerString);
If the guessed letter is 'i'
Actual Results:
??i?????????????
Expected Results:
??i??i??????i???
Samuel Philipp

Try this loop to replace all values, instead only the first one:

char[] answerStringChars = answerString.toCharArray();
for (int i = 0; i < answerString.length(); i++) {
    if (input.charAt(i) == guessChar) {
        answerStringChars[i] = guessChar;
    }
}
String newAnswerString = String.valueOf(answerStringChars);

This should do the job.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make the code compare 2 values in an array?

How do I tokenize a char array input into a char and a string?

How do I format the values of an array of `unsigned char` in hexadecimal?

How do I create array text boxes in Angular 2 and How to send array text boxes values to component

How do I create a function to change values ​in an array?

How do I add String to a char array?

How do I change file name from Hex into Char?

How do I change each char in a string but not change punctuations in C?

How do I change a char's values?

How do i change a StateObject Array in a forEach?

How do I assign the same array values to 2 different variables

How do I compare a a string array with a char array

How do I change the output values to 2 decimal places?

How do I change the character displayed in the 2D array?

How do I properly change the values of a Radiobutton?

how do I free this char** array?

How do I change id values in a numpy array to a string numpy array with a dictionary

How do I change the values of an multidimension array in JavaScript?

How do I insert char* to array of structures?

How do I read a stringstream into a char *[40] / char ** array?

How do I printout a 2 dimensional char array?

How do I change a particular int value in an array to a char in C?

How do I use tolower() with a char array?

How do I make it that a vector in an array doesn't change its values?

How do I change array values with dict keys?

How do I change values in a scriptable object?

How do I generate a 2D Array with values, each with neighbours of different, non-repeating values?

How do I check array values against other array values without O(N^2) complexity

C - how do I change and compare values within an array using pointers?