I'm writing a function in C that will take a string and remove all characters that are not a lowercase alphabetic character. I have this code written so far:
void strclean(char* str) {
while (*str) {
if (!(*str >= 'a' && *str <= 'z')) {
strcpy(str, str + 1);
str--;
}
str++;
}
}
When I pass it the string "hello[][]world" the function seems to mostly work, except the output is:
hellowoldd
When I make it print after every line that it enters the if statement, here is the output I receive:
hello][]woldd
hello[]woldd
hello]woldd
hellowoldd
It seems to be really close but I can't understand why it would be producing this output! The weirdest part is I have given the code to two other friends and it works fine on their computers. We are all running the same version of Linux (ubuntu 14.04.3), and are all using gcc to compile.
I'm not sure if there is an issue with the code that would cause inconsistency of the output, or if it's a compiler issue that's creating the problem. Maybe it has to do with strcpy on my machine compared to theirs?