0

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.

RapiDo
  • 3
  • 4
  • The only problem I could detect in this question is the use of English. – moffeltje Mar 20 '15 at 12:02
  • Could you make your problem more precise ? If your goal is to get all the characters from `available` which do not appear in `letters`, then you can do that very easily by iterating over `available` and doing an inner loop iterating over `letters` to check whether the character appears or not. If you can shorten your code and explain exactly what you want to achieve and where it doesn't work, it's easier for us to analyze. – Matt Ko Mar 20 '15 at 12:04

1 Answers1

0

Your replace_str function uses a static buffer that has a limited size and is not thread-safe. Don't do it that way.

So, if I understand correctly, you have a string of letters and another string, and want to construct yet another string from it from which the specified letters have been removed.

I recommend first constructing a lookup table:

char allowed_letters[256];

(you could micro-optimize by using a bit array with bitshifts and masks but that would save only few bytes of memory so it's not worth it)

Then, initialize allowed_letters using a for loop to all ones, and loop through the string letters and set allowed_letters[(unsigned char)letters[i]] = 0;

Now you have a lookup table which tells which letters are allowed and which are forbidden.

Then you need to consider where you want to store the result. One option would be to use another string buffer, perhaps allocated with malloc(strlen(available)+1). Or perhaps you want an in-place algorithm that overwrites the string of available letters.

Anyway, either way you need to manage an output pointer (outp) which points to the position in the output string (be it a separate memory block or the original available string), and and input pointer (inp) which points to the position of the input string you're reading.

Then do something like:

while (*inp)
{
  if (allowed_letters[(unsigned char)*inp])
    *outp++ = *inp++;
  else
    inp++;
}

and don't forget to '\0'-terminate the output string.

juhist
  • 4,210
  • 16
  • 33
  • Thank you, you were right, that I did not have to use str_replace.I invented it passes differently about finding and replacing letters. But the most help to solve all the problems that I used `allowed_letters[256] = "content"` instead `allowed_letters[] = "content"`. Because I found that the main problem I was "segmentation fault" and after compiling through gcc, because reviewed error in xcode I understand and etc. – RapiDo Mar 22 '15 at 00:44