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 19:17

Sepples considered harmful.

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

char table[256];

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

    if (argc < 3)
        return 1;

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

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

    for (i = 0, j = 0; argv[1][i]; ++i) {
        if (!table[(int)argv[1][i]])
            out[j++] = argv[1][i];
    }
    out[i] = 0;

    puts(out);
    return 0;
}


$ gcc dicks.c -Wall -Wextra -pedantic -ansi -o dicks && ./dicks "she sells sea shells on the sea shore" "se"
h ll a hll on th a hor

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