java - Resetting a variable to zero while in a loop -
i have code allows me take string , replace vowels increasing number.
for example, "abababababababababababa" give me "0b1b2b3b4b5b6b7b8b9b10b11". trying maintain number, 1 time goes past 9, resets 0 , starts counting 1 time again (so "0b1b2b3b4b5b6b7b8b9b10b11" instead read "0b1b2b3b4b5b6b7b8b9b0b1").
i can't figure out way within loop isn't convoluted. if there way of achieving wouldn't mind divulging, 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)); } homecoming word2; }
you can utilize modulus of 10 (% 10)
, wrap count after 9th digit starting again.
public static string getnumberstring(string s) { string word = s; string word1 = word.replaceall("[aeiouaeiou]", "@"); int c = 0; string word2 = word1; (c = 0 ; c <= word.length() ; c++) { word2 = word2.replacefirst("@", integer.tostring(c % 10)); } homecoming word2; }
output
0b1b2b3b4b5b6b7b8b9b0b1
java
No comments:
Post a Comment