2
I’m trying to create code to omit repeated characters in a row. I don’t know what’s going on and I’ve tried debugging for hours. Anyway, I’m trying to locate occurrences of consecutive repeated characters and then count more. My code sucks. How can I fix this?
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char Str[] = "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
unsigned long Len = strlen(Str);
/*for (unsigned long Pos = 0; Str[Pos]; Pos++) {
if (Str[Pos] >= 'A' && Str[Pos] <= 'Z') {
Str[Pos] += 'a'-'A';
} else if (Str[Pos] < 0) {
Str[Pos] = '_';
Len++;
} else {
switch (Str[Pos]) {
case '@':
Str[Pos] = 'a';
break;
case '8':
Str[Pos] = 'b';
break;
case '(':
Str[Pos] = 'c';
break;
case '3':
Str[Pos] = 'e';
break;
case '#':
Str[Pos] = 'h';
break;
case '1':
case '!':
Str[Pos] = 'i';
break;
case '&':
Str[Pos] = 'k';
break;
case '0':
Str[Pos] = 'o';
break;
case '?':
Str[Pos] = 'p';
break;
case '5':
case '$':
Str[Pos] = 's';
break;
case '7':
case '+':
Str[Pos] = 't';
break;
}
}
}*/ //Não é relevante
char PrevChar = *Str;
unsigned long Pos = 1;
while (Pos < Len) {
register unsigned long RChars = 0;
for (; Str[Pos+RChars] == PrevChar; RChars++);
if (RChars) {
memmove(Str+Pos, Str+Pos+RChars, (Len-=(RChars))-Pos);
}
if (!Str[RChars+Pos]) {
break;
}
PrevChar = Str[Pos];
Pos++;
}
Str[Pos] = '\0';
puts(Str);
return 0;
}
Upshot:
Terminated due to signal: SEGMENTATION FAULT (11)
264, 401, 666
s18446744073709551479, 399, 265
I see how long I try to solve my problem and I managed to put my head on the table, on the floor and on the wall underneath me, because it makes me want to cry.
thank you!!!!!!!!!!!
– user185234
@Asadefa If this answer solved your problem and you have no further questions, click the " " on the left of the answer to mark it as accepted/correct, which also marks your question as solved/answered. Otherwise, feel free to comment.
– Victor Stafusa