CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Jan 2010
    Posts
    14

    splitting a string

    Hi All,

    I am new to c++, I want to split the string and add some thing to it
    for example let say iam getting a input

    string old="company$45$0.07$29-09-2009$company2$34$0.09$21-09-2009"

    now i want a output where it should come like

    string new=" ['company',45,0.07,'29-09-2009'],['company2',34,0.09,'21-09-2009'] ";

    where i am splitting the string with $ char.

    Please help me out.

    Thanks in Advance

    Regards,

    Farooq.

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    you can use std::string.
    std::string has a nice set of functions which you could use.

    If you need more help,
    you can post your code in [code] ... [/code] tags.

    one thing I see is that the word 'new' is a reserved keyword, so you can't use it as an identifier.

  3. #3
    Join Date
    Jan 2010
    Posts
    14

    Re: splitting a string

    Hi as i said i have done with any code as new to c++ syntax and functions and i wrote new only for sample. so please help me in solving the issue.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: splitting a string

    Quote Originally Posted by farooq124in View Post
    Hi as i said i have done with any code as new to c++ syntax and functions and i wrote new only for sample. so please help me in solving the issue.
    string::find to find '$'
    string::substr to split the string

    Other than that, you're going to have to put forth a little bit off effort yourself towards writing a solution.

  5. #5
    Join Date
    Jan 2010
    Posts
    14

    Re: splitting a string

    Quote Originally Posted by Speedo View Post
    string::find to find '$'
    string::substr to split the string

    Other than that, you're going to have to put forth a little bit off effort yourself towards writing a solution.
    I said i am new to c++ simply saying use substring and find doesn't help me at all.....

    Please if any one knows a sample code for my problem please post it...

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: splitting a string

    Quote Originally Posted by farooq124in
    I said i am new to c++ simply saying use substring and find doesn't help me at all.....
    Speedo did not just tell you to use them, but also linked to references on how to use them.

    Quote Originally Posted by farooq124in
    Please if any one knows a sample code for my problem please post it...
    That is equivalent to spoonfeeding you a direct answer so no, at least not until you have shown more effort on your own.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jan 2010
    Posts
    14

    Re: splitting a string

    Quote Originally Posted by laserlight View Post
    Speedo did not just tell you to use them, but also linked to references on how to use them.


    That is equivalent to spoonfeeding you a direct answer so no, at least not until you have shown more effort on your own.
    see its not like that

    Code:
     
    I tried a sample code
    
    
    #include <stdafx.h>
    #include<string.h>
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <conio.h>
    #include <iostream>
    
    using namespace std;
    
    class Foo{
    public:
    Foo(int array_size, string nat_string);
    ~Foo();
    
    void showStringArray(){
    int i=0;
    int lengthofstring=0;
    while(act_string.find("$",posit)!=string::npos)
    {
    posit= act_string.find("$",posit);
    position_array[i]=posit;
    ++i;
    ++posit;
    }
    cout<<"substrings are....";
    //in this loop iam getting stack overflow...????
    for(j=0;j<size;j++)
    {
    if(j==0){
    substrings_array[j]=act_string.substr(intial,position_array[j]);
    intial=position_array[j]+1;
    }else{
    lengthofstring = position_array[j]-intial;
    substrings_array[j]=act_string.substr(intial,lengthofstring);
    intial=position_array[j]+1;
    }
    
    cout<<substrings_array[j];
    
    }
    
    }
    
    private:
    int *position_array;
    string *substrings_array;
    int size;
    string act_string;
    int intial,posit,i,j;
    
    };
    Foo::Foo(int array_size, std::string nat_string){
    intial =0;
    posit=0;
    j=i=0;
    size = array_size;
    act_string = nat_string;
    position_array = new int[size];
    size++;
    substrings_array = new string[size];
    }
    Foo::~Foo(){
    delete[ ] position_array;
    delete[ ] substrings_array;
    }
    
    int main(){
    string mystring= "this$123$0.07$29-09-1987$String$789$0.99$29-09-1999";
    int position=0,arraysize=1;
    while(mystring.find("$",position)!=string::npos){
    position = mystring.find("$",position);
    arraysize=arraysize+1;
    ++position;
    }
    Foo f1(arraysize, mystring);
    f1.showStringArray();
    scanf("&d",position);
    return 0;
    }
    
    
    So i am getting stack overflow in the for loop if j>7 please hlp me out and also i am printing cout<<substrings_array[j];
    
    instead of that i want to do like this
    
    cout<<"['"<<substrings_array[j]<<"',"<<substrings_array[j+1]<<","<<substrings_array[j+2]<<",'"<<substrings_array[j+3]<<"'],";
    j=j+3;
    
    I know it wont work but please help me in this as i want the output like ['this',123,0.07,'29-09-1987'],['String',789,0.99,'29-09-1999'];
    
    
    
    i know how can it be done in c#
    
    string all = "this$123$wer$qwe$this2$1234$qww$wer2";
                string[] alln=all.Split('$');
                string sample=string.Empty;
                for (int i = 0; i <alln.Length ; i++)
                {
                    sample =sample+ "['" + alln[i].ToString() + "'," + alln[i + 1].ToString() + "," + alln[i + 2].ToString() + ",'" + alln[i + 3].ToString() + "'],";
                    i = i + 3;
                }

    so its not like spoon feeding if u give me something i have to dig in it as i dont have time i am asking for help.

    Thanks in Advance
    Regards,
    Farooq.
    Last edited by farooq124in; January 29th, 2010 at 06:00 AM.

  8. #8
    Join Date
    Jan 2010
    Posts
    14

    Re: splitting a string

    Can any 1 help me plz...

  9. #9
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    Just convert '$' to ' '. after that place your string into a stringstream object. use the operator >> to extract each token placing them into a stl container. this is the simplest form. A simple google search will give more ways.
    http://www.google.com/search?hl=en&q...f&aqi=g-c1&oq=
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: splitting a string

    Quote Originally Posted by farooq124in
    I tried a sample code
    Good to see that you posted code in code tags, but you should also indent the code properly and tell us how does it not work.

    You might want to simplify things: write a program that given a string literal with the kind of input you expect, parses that string correctly.

    Quote Originally Posted by Joeman
    Just convert '$' to ' '. after that place your string into a stringstream object. use the operator >> to extract each token placing them into a stl container. this is the simplest form.
    However, a caveat with this method is that it may give you incorrect results if the original string may contain whitespace.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    Quote Originally Posted by farooq124in View Post
    Can any 1 help me plz...
    Write down how you'd do it on a piece of paper, and turn that into a code.
    It will do you some good.

    Here is a small, simple, and easy to understand code snippet that splits the string.
    I deliberately made errors in it for you to fix,
    the work of which I believe would benefit your debugging skills.
    Code:
    // std::vector is a dynamic array,
    // more flexible, safe, and easy to manage than a fixed array
    void Chunkify(const string& str, vector<string> result)
    {
        // to avoid magic number (it gives meaningful name to the number)
        const int DistanceToPeek = 1;
    
        // iterator is a companion to all STL containers (and std::string)
        // iterators behaves like a pointer (but they are not pointers)
        string::const_iterator iter = str.begin();
    
        // local object to add each chracter
        string chunk;
    
        // std::string::begin(), end() returns a compatible iterator
        while (iter != str.end())
        {
            // is the char equal to the delimiter, AND
            // and is not the last chracter?
            if(*iter == '$' && ( (iter + DistanceToPeek) != str.end()) )
            {
                // if yes, store the string into the dynamic array
                result.push_back(chunk);
    
                // clear the chunk string to start a whole new chunk of word
                // but how?
    
                // don't store the $ sign, just skip to the next char
                ++iter;
            }
    
            // anything but the $, we store it to the temporary string
            chunk.push_back(*iter++);
    
            // we increment the iterator to search for...
            iter += 2;
    
            // check to see if the last string has been read
            if(iter == str.end())
            {
                // if so, we store the last piece of the chunk
                // and let the scope clear (destroy) the variable named chunk
                result.push_back(chunk);
            }
        }
    }

  12. #12
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    Quote Originally Posted by laserlight
    However, a caveat with this method is that it may give you incorrect results if the original string may contain whitespace.
    I assumed it wouldn't have spaces. if it does, this way will work

    Place your string into a stringstream object. use the std::getline with the delimiter of '$' to extract each token placing them into a stl container. this is even more simple than what I said before.

    What I said can be done in 5 lines of code
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  13. #13
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    Quote Originally Posted by Joeman View Post
    What I said can be done in 5 lines of code
    What? 5 lines? Give me a break Joeman!

  14. #14
    Join Date
    Jun 2008
    Posts
    592

    Re: splitting a string

    I am serious
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  15. #15
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: splitting a string

    Quote Originally Posted by Joeman View Post
    I am serious
    Haha, No.
    what I meant was a man with your experstise should do it with a single line inside a function

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured