Help with Strings -- Please read

This is a discussion on Help with Strings -- Please read within the C++ Programming forums, part of the General Programming Boards category; Okay, I have a problem. I have a string which contains the following. 'none$none$south.dat$none' How would I seperate that string ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Exclamation Help with Strings -- Please read

    Okay, I have a problem. I have a string which contains the following.

    'none$none$south.dat$none'

    How would I seperate that string ($ being the seperator) into four seperate strings?

    I learn by example, so please jot me down some code =) I'm sure it's not too hard, however I am not too familiar with C++ strings, as I am an ex-VB coder.

    Better yet, email me with a reply.
    toolfan@bellsouth.net

    Thanks,
    ~Drakkath

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Code:
    int x, y, strcount, length;
    char yourstring[25] = "none$none$south.dat$none";
    char *seperated[4];
    y = 0;
    strcount = 0;
    length = 0;
    for(x = 0; yourstring[x] ; x++)
    {
    	if (yourstring[x] == '$')
    	{
    		seperated[strcount] = new char[length+1];
    		for(y = 0; y < length ; y++)
    			seperated[strcount][y] = yourstring[x-length+y];
    		seperated[strcount][y] = '\0';
    		strcount++;
    		length = 0;
    	}
    	else
    		length++;
    }
    for(y = 0; y < length ; y++)
    	seperated[strcount][y] = yourstring[x-length+y];
    seperated[strcount][y] = '\0';
    not sure if this is a good way to do what you want, but it should work for any string with a few modifications
    *edited to add proper spacing*
    Last edited by Syneris; 03-26-2002 at 11:33 PM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    24
    You might also want to try the getline function if working with a input file "infile.getline(variable,4,'$')" this will read "none" into variable for 4 spaces and stop at the $ sign. You could also make the 4 a 5 or 6 and it would still stop at the $ sign. but allow for a bigger string. Try checking out ignore() to help aid the get line and maybe check the string.h lib for some ideas? Hope this helps

    Skeptic

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    It is a file input. However, the key is that the $ may appear after any number of characters, so I need a function to stop at $, not at 4 or 5 or any characters for that matter, get what I mean?

    Oh well, thanks for the code and the replies!

    Best of luck to you all,
    Drakkath

  5. #5
    Unregistered
    Guest
    using getline() similar to this:

    const int largestNumberOfCharacersExpected = //whatever;
    char bufferName[largestNumberOfCharactersExpected];
    ifstream.fin("myFile.txt");
    fin.getline(bufferName, largestNumberOfCharactersExpected, '$');

    will allow you to read characters into bufferName until one of the following happens:

    a) the largestNumberOfCharacters expected - 1 number of characters have been read in
    b) a $ char has been encountered
    c) end of file character has been encountered

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Post

    Okay, that would work, but I need exitString[51] to be able to be split into exitNorth[12], exitSouth[12], exitEast[12], and exitWest[12].

    How could I do that with getline? That seems like the most logical idea...

    Thanks,
    Drakkath

  7. #7
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    couldn't you just use strtok() ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. read certain strings from file
    By shatred in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2006, 03:48 PM
  5. cin strings till eof
    By bfedorov11 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2003, 08:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21