/*
Given AAABBGFF should get an output 3{A} 2{B}1{G}2{F}
*/
//解法一
#include <iostream>
void transStr(char* s)
{
int len = strlen(s);
char curChar = ' ';
char preChar = ' ';
int count = 0;
for(int i = 0; i < len; i++)
{
curChar = s[i];
if(curChar != preChar)
{
if(i != 0)
{
printf("%d", count);
printf("{%c}", preChar);
}
count = 0;
preChar = curChar;
}
count++;
}
printf("%d", count);
printf("{%c}", preChar);
};
int main()
{
char str[20] = "AAABBGFF";
transStr(str);
return 0;
}
//解法二
#include <iostream>
void transStr(char* s)
{
int i = 0;
int count = 0;
while(i < strlen(s))
{
while(s[i] == s[i + 1])
{
i++;
count++;
}
count++;
printf("%d{%c}", count, s[i]);
count = 0;
i++;
}
};
int main()
{
char str[20] = "AAABBGFF";
transStr(str);
return 0;
}