Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

small string challenge

Name: Anonymous 2012-05-04 10:39

hi /prog/

I need to make a function that takes two strings, and removes all characters from the first string that are in the second string; we get marked on how efficient our code is. This is what I got so far, it's pretty shit and apparently theres a much better way to do it.


#include <iostream>
#include <string.h>

void rmFromString(std::string & toBeProcessed, const std::string & toBeIgnored)
{
        size_t found;
      std::string newString;

      found = toBeProcessed.find_first_not_of(toBeIgnored);

      while (found != std::string::npos)
      {
            newString += toBeProcessed[found];
            found = toBeProcessed.find_first_not_of(toBeIgnored, found + 1);
      }

    toBeProcessed = newString;

}

int main()
{
    std::string toBeProcessed = "she sells sea shells on the sea shore",
        toBeIgnored = "se";

    rmFromString(toBeProcessed, toBeIgnored);

    std::cout << toBeProcessed << std::endl;
}


anyone want to point me in the right direction?

Name: Anonymous 2012-05-04 23:23

>>9
Good point, but if the input is in ASCII, sign extension won't be an issue and the only problem is that the table is twice as big as it needs to be. If it's not, the algorithm is likely broken anyway. (Are there any single-bit ASCII extensions still in common use?)
Getting rid of the branch is probably a good idea, but you've introduced new problems. The loop through the table is unnecessary, table[(unsigned int)argv[2][i]]--; should really be table[(unsigned int)argv[2][i]] = 0; (or repeated characters in the second string break things), and out[j] = 0; forgets that j has changed meaning.
So:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int table[256];

int main(int argc, char **argv)
{
    unsigned int i, j;
    char *out, *pout;

    if (argc < 3)
        return 1;

    for (i = 0; argv[2][i]; ++i)
        table[(unsigned int)argv[2][i]] = -1;

    out = malloc(strlen(argv[1]) + 1);

    for (pout=out,i=0; argv[1][i]; ++i) {
        j = argv[1][i];
        *pout = j;
        pout += 1 + table[j];
    }
    *pout = 0;

    puts(out);
    return 0;
}


It's possible this could be further improved by replacing the assignment to our output string with a putchar, since we don't actually need to save the string; that way we don't even need to malloc anything and we can omit the call to strlen, which saves a pass through the first string, but it does reintroduce the branch.
I don't know if that's a net savings and I'm not about to find out.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List