Paul(Zhenbo Xu)
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
You mean 48 + count may be out of range of char? It would be more understandable by replacing it with '0' + count.
- Paul(Zhenbo Xu) October 22, 2012Comment hidden because of low score. Click to expand.
0
of 0 vote
#include <iostream>
#include <cstring>
using namespace std;
void replaceDuplicate(char *a) {
int len = strlen(a);
int count = 1;
int iReplace = 0;
char lastChar = '\0';
for (int i = 0; i <= len; i++) {
char c = a[i];
if (c == lastChar) {
count ++;
} else {
if (count > 1) {
a[iReplace++] = lastChar;
a[iReplace] = 48 + count;
count = 1;
}
lastChar = c;
iReplace++;
}
}
a[iReplace] = '\0';
}
int main(void) {
char a[100];
cin >> a;
replaceDuplicate(a);
cout << a;
return 0;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
I see what you mean. The modification can be converting 'count' to string and writing it back to original array.
- Paul(Zhenbo Xu) October 23, 2012