LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   string question (https://www.linuxquestions.org/questions/programming-9/string-question-375204/)

AquamaN 10-20-2005 05:08 PM

string question
 
Ok, so I am trying to get all the length of words in a file, but some words are special like "copy-cat" and I need to break them up into just "copy" and "cat" and it's been ages since I've worked with strings. How can I remove the "-" (or any other character like a "/", "." or "( )") from the word once I've taken it into a string (an actual string, not char)? I know the answer is probably trivial but for some reason I can't remember... I think I need more sleep. Thanks for any help.

-aquaman

AquamaN 10-20-2005 05:14 PM

Or is there a way that when I take them into my

Code:

list<input> inList;
that I can just have it ignore those characters so each word gets broken up automatically? Just a thought.

lowpro2k3 10-20-2005 06:17 PM

I assume you're talking about C++, but without your second post there is no way of being able to tell. Just about every language has strings you know...

If thats the case it depends whether you're using std::string's or C-style char* strings. You didn't state that one either.

C style, I would scan once, find 'n' number of bad items, make a new array of (size - n), and copy the valid ones over.

Code:

int i, n, len;
char *orig, *copy;
orig = "a/string/with/a/.";

for(i = 0, n = 0, len = strlen(orig); i < len; i++)
  if(orig[i] == '/' || orig[i] == '.')  // check others if you want
    n++;

if(n > 0) {
  copy = new char[n];
  // and copy the orig string over, I have to run, sorry
} else {
  // string has no illegal chars'
}



All times are GMT -5. The time now is 03:21 PM.