I need to create a program in java in which when the input given is say:
hhllyyjhhh
the output should be
h2l2y2j1h3
instead i get the output
h2l2y2j1
I know the reason why, but please tell me how should I correct it or may be tell a new logic.
in the following code T
is the character array and ans
is an empty string.
int counter=0;
for(int i=0;i<T.length;i++)
{
for(int j=i;j<T.length;j++)
{
if(T[i]==T[j])
{
counter++;
}
else
{
ans=ans+T[i]+counter;
i=j-1;
counter=0;
break;
}
After the for loops
you need to append the last character values to the ans
as currently it is getting skipped.
if(counter>0) {
ans=ans+T[T.length-1]+counter
}
Or you can do only with just one for loop like this:
char[] T = "hhllyyjhhh".toCharArray();
int counter = 1;
StringBuilder out = new StringBuilder();
for(int i=0;i<T.length-1;i++) {
if(T[i]==T[i+1]) {
counter++;
} else {
out.append(T[i]).append(counter);
counter=1;
}
}
if(counter>0) {
out.append(T[T.length-1]).append(counter);
}
System.out.println(out.toString());
The issue is you don't add the last character if your count matches all the way, I suggest you change your approach slightly and try and scan forward before you append the character and count (basically move your else outside the inner loop) -
char[] T = "hhllyyjhhh".toCharArray();
String ans = "";
for (int i = 0; i < T.length; i++) {
int count = 1;
while (i + count < T.length && T[i + count] == T[i]) {
count++;
}
ans += T[i] + String.valueOf(count);
i += count - 1;
}
System.out.println(ans);
Produces your requested output here.
Check the code below. The comments are explaining the logic.
public static void main(String[] args) {
// invoking our method
System.out.println(getCompressedString("hhllyyjhhh"));
}
private static String getCompressedString(String rawString) {
final StringBuffer sb = new StringBuffer();
final char[] rawStringChars = rawString.toCharArray();
// the first symbol
int counter = 1;
char processingChar = rawStringChars[0];
// processing the rest of string symbols
for (int i = 1; i < rawStringChars.length; i++) {
// if there's another symbol
if (processingChar != rawStringChars[i]) {
sb.append(processingChar);
sb.append(counter);
// setting new processing char and new counter
processingChar = rawStringChars[i];
counter = 1;
} else {
// if there's the same symbol
counter ++;
}
}
// writing "tail" part
sb.append(processingChar);
sb.append(counter);
return sb.toString();
}
You need to appened last character and its count outside your for loop because else part does not handle the appending for last character. Here i have tweaked your code to get required result.
public static void main(String[] args) {
char T[] = { 'h', 'h', 'l', 'l', 'y', 'y', 'j', 'h', 'h', 'l', 'a' };
String ans = "";
int counter = 0;
int j = 0;
for (int i = 0; i < T.length; i++) {
if (j == T.length - 1) {
if (T[i] != T[j - 1]) {
counter = 1;
}
break;
}
for (j = i; j < T.length; j++) {
if (T[i] == T[j]) {
counter++;
if (j == T.length - 1) {
break;
}
} else {
ans = ans + T[i] + counter;
i = j - 1;
counter = 0;
break;
}
}
}
if (counter > 0) {
ans = ans + T[T.length - 1] + counter;
}
System.out.println(ans);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.