Yes,
This may sound like the rest, but this actually has to do with something different. I'm not quite understanding why im running into the problem. Firstly, here is the code:
Code:
#include <stdio.h>
#include <string.h>
void removeLetters(char *str, int n);
int main() {
char str[64] = {"We Remove First And Last Three Letters Now"};
removeLetters(str, 3);
printf("%s\n", str);
return 0;
}
void removeLetters(char *str, int n) {
int i, len, pos = 0;
len = strlen(str);
// Go through loop starting at 'n'
for (i = n; i < len && pos != len-n*2; i++, pos++) {
str[pos] = str[i];
str[pos++] = '\0';
}
}
Next, this is not homework, I have done this code inside out a billion times, but this time it hasn't worked. Though my second favorite way to do this is:
Code:
void removeLetters(char *str, int n) {
memcpy(str, &str[n], strlen(str));
str[strlen( str )-n] = 0x00;
}
Though I am trying a different approach. My problem so far is that "pos" never increments in my for loop statement but if I put pos++ inside the statement, not the call it works fine. Example:
Code:
for (i = n; i < len && pos != len-n*2; i++) {
str[pos] = str[i];
pos++;
str[pos] = '\0';
}
To clarify what I want to do is trim off the first 3 and the last three letters of my sentence. I just dont like having to do at the end the whole "str[strlen(str)-n]" thing. That's why I have tried to write another way.
Is there a simple explanation why "pos" won't increment in my for loop call?
Thank you for your time,
- Stack Overflow