Thread: how to cut a string into smaller strings?

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

    how to cut a string into smaller strings?

    hey fellas:

    i got a string into my proggy and want to cut it down.. i've investigaed strstr but that seems to retun a string you are looking for.. are there any other functions i may use?

    the string resembles this

    [code]

    one : two three four five


    [\code]

    i want to cut the one out, the two the three and so on.. into their own unique strings... any suggestions welcome..

    thanks
    SS3X

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    in a header file <string.h>

    substr(Pos, Len);

    Returns the substring that begins at position Pos and contains Len characters..



    hope this helps...

    matheo917

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    thanks for the reply!

    another question though
    assuming Len is a number.. what if i don't know the number of the characters?

    i need to arbitrarily cut it down into sperate strings.. seperated by a white space...

    thanks
    SS3X

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    if you're talking about about unknown Position then you can come up with some kind of a "home-made" algorith that searches for a disired condition, if such is found then make correct cuts and perhaps iterations

    for example:

    in the same header <string.h>

    function:

    strchr( SomeString, char(32) );

    returns a pointer to the first occurrence of char(32) "being a space" in SomeString, otherwise if not encountered it returns NULL...

    so i don't solve the whole problem for you here's couple hints:

    strings are just another words for character arrays, if you know what they are.....the only difference is that strings have a terminating character at the end and character array don't unless you give it to them....

    so the string "Hello" actuall is "Hello\0" and "\0" being the terminating character....

    with that in mind you can create some kind of a i.e "for" loop that traverses, searches a string character by character untill it gets to the end --> "\0"......

    in a same header <string.h>

    function:

    length(SomeString);
    or
    size(SomeString);

    returns number of characters in a string in question....

    hope this cleared it up a bit...

    keep up the good work

    Regards,
    matheo917

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Or if you wanted to be entirely too elegant about it, you could make the string into a stringstream (yup) and then use standard stream extraction on it, because that is whitespace-delimited.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Actually, I think the function is strlen();
    Just loop through your string and add each character to a new string until you come across a whitespace. Try using isspace(char) in ctype.h instead of comparing the char to ' '.

  7. #7
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I think the function you want is string.substring which returns a string using two element positions so that you just search for the space char and get it's postion then find the next one and get the word between those two spaces, then move on to the next space, etc.
    there's also a string stream object that you can use to extract from strings word at a time using the << operator


    PHP Code:
    //compiled in VC++ 6 as a 32bit console app


    #include <iostream>
    using namespace std;
    #include <string>



    int main()
    {

    string Test123=" one two three";


    string myBuf;//this will be where I put the piece of the string



    int prevPos=Test123.size()-1;//this will store the end of the sub string
    int nextPos=Test123.size()-1;//and the beginning of the sub string
    //whatever is inbetween will be the word
    //I initilize them to both be at the end of the string



    //rfind returns string.npos if the string we search for is not found
    //since I searched for a space then if it does not find it then it has reached the end
    //but that just means there might be another word to read since the line might not end in a space

    while((Test123.npos != nextPos)&&(nextPos))//this would mean we've reached the end of the string, the AND nextPos would be a case where the string contained a space at the begining
    {
        
    prevPos=nextPos;//we're fixing to move nextPos to the next space but we want to know where it came from

        //rfind starts searching for a string at the right end of the string and moves left until it finds it
        //then returns the element position of that string we searched for
        
    nextPos=Test123.rfind(" "prevPos);
        
        if(
    nextPos==Test123.npos)//this tells me it didn't find a space last time so we are now at the beginning of the file
        
    {
        
    nextPos=0;
        
    myBufTest123.substr(nextPos, (prevPos+1));//returns a string between next and prev
        
    }

        else
        {
        
    nextPos+=1;//this is a technicality, you have to move it back a little so that you don't get the space

        //stores the string starting at nextPos, and stores number of chars defined by second param
        
    myBufTest123.substr(nextPos, (prevPos-nextPos+1));
        
    //try running debug, go to View, Debug Windows, Watch, type in Test123.substr(4,3) and hit enter, play with the parmeters
        
        
        
    if(nextPos==1)//this would be if we just read the last word and the string happens to contain a space at the beggining
            
    nextPos+=1;//add 1 so that we don't try to subtract 2 from 1, this would mean nextPos becomes 0 in the next statement        

        
    nextPos-=2//and then moves it over the space so that we don't get stuck finding it over and over again
        
    }


        
    //for demonstration puroses :>
        
    cout<<myBuf<<" ";
        
    //notice that rfind searches from right to left so you get the words out in right to left order
    }




    return 
    0;

    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    thanks guys for all the help.. i've made some progress and started a new thread for help on that..

    thanks!
    SS3X

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Help! Homework using Strings
    By wco5002 in forum C++ Programming
    Replies: 16
    Last Post: 09-27-2007, 03:53 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Strings - char mystring[n] vs. string mystring
    By Diablo84 in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2005, 05:54 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM