Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ Question, tailoring a string!?

here is my code, trying to make it so that it will edit a string like 'P0oP'

so that it'll be turned into

'pop'

it's throwing me a core dump right now.

string fixString(string s){

for(int i=0; i<s.length(); i++){

char c = s[i];

if(c>='a' && c<='z'){

s[i] = tolower(s[i]);

}

else{

s.erase(i,1);

}

}

}

thanks in advance!

Update:

ok so the basis of my program is that it reads in a bunch of strings from a text file, and i want it to work with any kind of words.

so it could have a sentence like "Gee - Look over there!"

and i want it to read into a vector of strings so that it looks like with index - value

0 - gee

1 - null (hyphen is erased)

2 - look

3 - over

4 - there (exclamation is dead)

my code is the following

int main(int argc, char **argv){

//using vector to read in all words

vector<WordNode> word_storage;

//vector containing stopwords

vector<string> STOP_WORDS;

//fill stop words vector

STOP_WORDS = getStopWords();

//used to keep track of how many times the loop is cycling

unsigned int count = 0;

//argument for ARGC...

ifstream fin(argv[1]);

//fill word_storage

while(!fin.eof()){

string word;

fin >> word;

//cout << word << endl;

word = fixString(word);

2 Answers

Relevance
  • ?
    Lv 5
    10 years ago

    Someone else pointed out most of the problems.

    decrement i or could skip.

    only preserves lower case letters.

    no return.

    I ran your code with only adding the return of s and it worked just fine on "p0op". So if it is still crashing use gdb's backtrace or visual c++'s call stack to see where it went wrong.

  • 10 years ago

    After you erase a letter, you should decrement i, otherwise you can miss some of the characters.

    Also, in your if statement, i think you should be comparing to 'A' and 'Z'.

    All that being said, I think the core dump might be because you have no return value for the function.

Still have questions? Get your answers by asking now.