|
-
January 27th, 2010, 07:32 AM
#1
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.
-
January 27th, 2010, 07:39 AM
#2
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.
-
January 27th, 2010, 08:15 AM
#3
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.
-
January 27th, 2010, 09:32 AM
#4
Re: splitting a string
 Originally Posted by farooq124in
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.
-
January 28th, 2010, 01:15 AM
#5
Re: splitting a string
 Originally Posted by Speedo
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...
-
January 28th, 2010, 01:24 AM
#6
Re: splitting a string
 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.
 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.
-
January 28th, 2010, 01:33 AM
#7
Re: splitting a string
 Originally Posted by laserlight
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.
-
January 29th, 2010, 06:03 AM
#8
-
January 29th, 2010, 06:53 AM
#9
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
-
January 29th, 2010, 07:30 AM
#10
Re: splitting a string
 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.
 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.
-
January 29th, 2010, 08:08 AM
#11
Re: splitting a string
 Originally Posted by farooq124in
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);
}
}
}
-
January 29th, 2010, 05:08 PM
#12
Re: splitting a string
 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
-
January 29th, 2010, 10:59 PM
#13
Re: splitting a string
 Originally Posted by Joeman
What I said can be done in 5 lines of code 
What? 5 lines? Give me a break Joeman!
-
January 29th, 2010, 11:02 PM
#14
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
-
January 29th, 2010, 11:06 PM
#15
Re: splitting a string
 Originally Posted by Joeman
I am serious 
Haha, No.
what I meant was a man with your experstise should do it with a single line inside a function
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|