I am reading a URL (which is string) and searching for a pattern (consecutive occurrences of the /
character). If I find a matching pattern, I want to replace it with a single /
and copy rest of the characters as they are. For example: If the input string is http://www.yahoo.com/
, I need to produce output http:/www.yahoo.com/
by removing the extra /
since that character occurred twice, consecutively.
Here is the program:
int main() {
int i, j;
bool found = false;
unsigned char *str = "http://www.yahoo.com/";
int len = strlen(str);
for (i = 0; i < len - 1; i++) {
if ((str[i] == '/') && (str[i + 1] == '/')) {
found = true;
break;
}
}
if (found) {
for (j = i + 1; j <= (len - i - 2); j++) {
str[j] = str[j + 1];
}
}
return 0;
}
But this program is generating a segmentation fault. Where is the problem in this code? Any idea how to fix it? Any alternate simple implementation for this?