shunfurh
编程介的小学生
采纳率92.7%
2019-03-02 23:59

求问一个英文字母的编码的问题,采用C语言如何才能实现的呢

Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:

  1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

  2. 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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_44684838 pickaBoo 2年前
    #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;
    }
    
    
    点赞 评论 复制链接分享

相关推荐