- 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

13th Jan 2018, 8:17 AM
Devang Prajapati
Devang Prajapati - avatar
7 Answers
+ 5
Ok @Devang then I will try to help you if I can.
13th Jan 2018, 8:23 AM
Akash Pal
Akash Pal - avatar
13th Jan 2018, 8:36 AM
Akash Pal
Akash Pal - avatar
+ 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
13th Jan 2018, 8:26 AM
Dan Walker
Dan Walker - avatar
+ 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!
13th Jan 2018, 8:37 AM
Dan Walker
Dan Walker - avatar
- 1
@Akash i need help...
13th Jan 2018, 8:20 AM
Devang Prajapati
Devang Prajapati - avatar
- 1
@DanWalker give any logic.
13th Jan 2018, 8:30 AM
Devang Prajapati
Devang Prajapati - avatar