- 1
Java program to convert AABBB string to 2A3B.
Java program which gives output 2A3B when user inputs AABBB in a one single String. Another example : input string: CCCCBBB Output string: 4C3B
7 Answers
+ 5
Ok @Devang then I will try to help you if I can.
+ 5
https://code.sololearn.com/cUv4Hw6HEtXP/#java
Here is my code @Devang
+ 2
Have a go, post what you have and we can help :)
A few tips for how I might do it.. turn a string to a char array, and loop through the array. If the next letter is equal to the current one, increment a counter otherwise add what letter/number you currently have to a StringBuilder and reset counter to one. loop until we hit the last letter
+ 1
As I said, use a loop. Could be a normal or enhanced for loop or a while loop if you use an Iterator. You're comparing individual characters in a string so use String.toCharArray() or String.charAt(i) (use Google to check out the docs on these methods if you're not familiar with them)
so some pseudocode for the next bit:
if (this and next letter are the same)
add to counter
else
put counter and THIS letter in StringBuilder
reset counter to 1 (we have at least one of 'next' letter)
ensure loop doesn't make you go outside the index range!
- 1
@Akash i need help...
- 1
@DanWalker give any logic.