Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:
Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
If the length of the sub-string is 1, '1' should be ignored.
Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
Output
For each test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
#include<stdio.h>
#include<string.h>
char str[10001];
char result[10001];
int main()
{
int numTest,i,j,k;
int strLen;
int resIdx;
scanf("%d",&numTest );
while(numTest--) {
scanf("%s",str);
strLen = strlen(str);
resIdx = 0; //initialize the result index for each test Case
for(i=0; i<strLen; i++) {
for(j=i, k=0; j<strLen ; k++, j++) {
if(str[i]!=str[j])
break;
}
if(k>1) {
result[resIdx++]='0'+k; //convert the number into char
}
result[resIdx++]=str[i]; // copy the original char
i=j-1; //move the i to the point before next different point
} // the i++ in the outer loop will move i to next differnt point
result[resIdx]=0; // null the end of each result, since it maybe shorter than last result
printf("%s\n",result);
}
return 0;
}