I'm doing a function in C, which I of the field char letters[]
, which stores the features that I want in the char available[]
delete / omit just that in the field were not.
I found here stackoverflow replace function that I overwrite the string. The function works as I want, but I have put it out there for the show.
The problem arises in function calls and the call letters of char letters[]
.
REPLACE FUNCTION
char *replace_str(char *str, char *orig, char *rep) {
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) return str;
strncpy(buffer, str, p-str);
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
Calling a function in my main.
int main() {
char letters[] = "arpstxgoieyu";
char available[] = "abcdefghijklmnopqrstuvwxyz";
getAvailableLetters(letters, available);
}
In this case, the output should be: "bcdfhjklmnqvwz" but function return me all the letters.
The function which should override the characters of char available[]
existing in the char letters[]
void getAvailableLetters(char letters[], char available[]) {
char copy[30][30];
memset(copy, '\0', sizeof(copy));
strcpy(copy[0], available);
int d = 1;
for (int i = 0, length = (int)strlen(letters); i < length; i++, d++) {
strcpy(copy[d], replace_str(copy[i], &letters[i], ""));
}
strcpy(available, copy[d-1]);
}
Problem is with &letters[i]
. Because of the need to take every variable just one point I could just replace it in. If I use the '&', as a function of the 'replace_str' in char *orig
is a sample of the type \237
and immediately replace function performs no change.
I found that if you give strcpy(copy[d], replace_str(copy[i], "a", ""));
so it works, but again do the whole alphabet over switch, certainly do not.
So please, how to deal with that I did not have to switch to each letter and do it this way effectively. Thanks.