This is the code that I have been creating in order to finish some other project of mine. I have been attempting this for a while, and I am thinking that the variables declared within the loops are not being accessed if they are too many while loops deep, so I don't think that I can accomplish this through any simple means. It would be very helpful if someone could show me why this code didn't give me the result that I hoped it would.
char letter, D;
int lengthString, position, positionInCat, textLength, repetitions;
String cat = "cat", newCat = "", text, reversingText, reversedText = ""; //Goal: change "cat" to "cad"
D = 'D';
lengthString = cat.length();
position = 0;
positionInCat = 2; // what I am looking to alter. For example
//position in cat = 2. 2 becomes a D or any other character. The output should //be: cDt
reversingText = cat;
repetitions = reversingText.length();
textLength = reversingText.length() - 1;
while (repetitions > 0) { // reverses the text
letter = reversingText.charAt(textLength);
reversedText = reversedText + letter;
repetitions--;
textLength --;
}
letter = ' ';
cat = reversedText; // applies the reversion
while (lengthString > 1) {
while (lengthString != positionInCat) { // changes cat to what I want the outcome to be: cDt
letter = cat.charAt(position);
newCat = newCat + letter; // rebuilding CAT
position++;
lengthString--;
}
newCat = newCat + D; // Altering the cat
position++;
lengthString--;
while (lengthString > 0) {
letter = cat.charAt(position);
newCat = newCat + letter;
position++;
lengthString--;
} }
letter = ' ';
cat = newCat;
reversingText = cat; // giving the reversingText its work
repetitions = reversingText.length();
textLength = reversingText.length() - 1;
while (repetitions > 0) { // unreverses the reversed text
letter = reversingText.charAt(textLength);
reversedText = reversedText + letter;
repetitions--;
textLength --;
}
cat = reversedText; // applies the reversion
System.out.print(cat); // what my work should have given me is cDt
}
} // This is my attempt at it, but I can only get out "taccDt" with it, instead of "cDt"