I have this code that allows me to take a string and replace all the vowels with an increasing number.
For example, "abababababababababababa" would give me "0b1b2b3b4b5b6b7b8b9b10b11". I am trying to keep it so that the number, once it goes past 9, resets to zero and starts counting back up again (so that "0b1b2b3b4b5b6b7b8b9b10b11" would instead read "0b1b2b3b4b5b6b7b8b9b0b1").
I can't figure out a way to do it within the loop that isn't fairly convoluted. If there is some way of achieving this that someone wouldn't mind divulging, that would be greatly appreciated.
public static String getNumberString( String s)
{
String word = s;
String word1 = word.replaceAll("[AEIOUaeiou]", "@");
int c = 0;
String word2 = word1;
for( c = 0; c <= word.length(); c++)
{
word2 = word2.replaceFirst("@", Integer.toString(c));
}
return word2;
}
You can use Modulus of 10 (% 10)
, this will wrap the count after the ninth digit to starting again.
public static String getNumberString(String s) {
String word = s;
String word1 = word.replaceAll("[AEIOUaeiou]", "@");
int c = 0;
String word2 = word1;
for (c = 0 ; c <= word.length() ; c++) {
word2 = word2.replaceFirst("@", Integer.toString(c % 10));
}
return word2;
}
output
0b1b2b3b4b5b6b7b8b9b0b1
How about using modulo?
word2 = word2.replaceFirst("@", Integer.toString(c % 10));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With