I am coding a program, that deletes all redundant /
in the path of the file. In other words ///a/////b
should become /a/b
.
I can delete all repeated /
, but can't fully wrap my head about how to leave only one slash WITHOUT any standard function or indexation operation.
Can someone fix my code, please?
#include <stdio.h>
// ///a/////b
// a/b/a
// aa///b/a
void normalize_path(char *buf) {
int k = 0, counter = 0;
for (int i = 0; buf[i]; i++) {
buf[i] = buf[i + k];
if (buf[i] == '/') {
++counter;
if (counter > 1) {
--i;
++k;
} else {
++k;
}
} else {
counter = 0;
}
}
}
int main() {
char path[100];
scanf("%s", path);
printf("String before the function: %s
", path);
normalize_path(path);
printf("String after the function is used: %s
", path);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…