int main(int argc, char* argv[])
{
char s[256] = {0};
char prevChar = {0};
std::cin >> s;
int len = strlen(s);
prevChar = s[0];
int count = 1;
for (int i = 1; i < len+1; i++)
if (s[i] == prevChar) {
count++;
} else {
printf("%d %c\n", count, prevChar);
count = 1;
prevChar = s[i];
}
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
int main(int argc, char* argv[])
{
char s[256] = {0};
char prevChar = {0};
std::cin >> s;
int len = strlen(s);
prevChar = s[0];
int count = 1;
for (int i = 1; i < len+1; i++)
{
if (s[i] == prevChar) {
count++;
} else {
printf("%d %c\n", count, prevChar);
count = 1;
prevChar = s[i];
}
}
return 0;
}