•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,558 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,737 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4596 | Replies: 25
![]() |
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 0
Well, I wonder HOW to make an explode() function from PHP in C++
For those who don't know.
explode($separator, $string) breaks given $string through given $separator
for example:
Each word is put into the array and has own index.
now... have someone any idea how to make it in C++ ?
Do not show any examples from this forum.
I already saw all and all are bad (doesn't match to the requirements)
For those who don't know.
explode($separator, $string) breaks given $string through given $separator
for example:
$string = "Hello world!";
$result = explode(" ", $string);
echo $result[0]; //IT will give us HELLO word
echo $result[1]; //IT will give us WORLD! wordEach word is put into the array and has own index.
now... have someone any idea how to make it in C++ ?
Do not show any examples from this forum.
I already saw all and all are bad (doesn't match to the requirements)
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 0
I tried this:
http://www.daniweb.com/techtalkforums/thread66264.html
and this:
http://www.daniweb.com/techtalkforums/thread20378.html
Both are not what I wanted to...
http://www.daniweb.com/techtalkforums/thread66264.html
and this:
http://www.daniweb.com/techtalkforums/thread20378.html
Both are not what I wanted to...
PHP is open source. You can browse through its source code and see the way it is implemented.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
> Both are not what I wanted to...
Great, out of all possible solutions, you've narrowed the field by two solutions.
Still leaves a hell of a lot of possible solutions out there.
Care to actually put some effort in towards showing what your ideal solution might be, rather than repeatedly posting "not it".
We're not here to guess what you're thinking, nor play "20 questions" to find such stuff out.
Great, out of all possible solutions, you've narrowed the field by two solutions.
Still leaves a hell of a lot of possible solutions out there.
Care to actually put some effort in towards showing what your ideal solution might be, rather than repeatedly posting "not it".
We're not here to guess what you're thinking, nor play "20 questions" to find such stuff out.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 0
@Up
1. Get string wich user provided
2. break it through given separator
3. Each breaked put into array. Give to each word own index nr.
4. Each variable that function belongs to gives each word
e.x
That's what you wanted ?
1. Get string wich user provided
2. break it through given separator
3. Each breaked put into array. Give to each word own index nr.
4. Each variable that function belongs to gives each word
e.x
char func = explode(char separator, char string); cout func[0]; //1st word cout func[1]; //2nd word
That's what you wanted ?
•
•
Join Date: Dec 2006
Location: india
Posts: 1,045
Reputation:
Rep Power: 9
Solved Threads: 157
you do not have to make one. it is available in libraries.
either the boost string algorithms or the boost tokenizer could be used.
here is an example using the boost string algorithms split. as you can notice, c++ libraries are
far more powerful and expressive. in this example, we are splitting on different criteria and placing the
result into different types of containers. and we are using one function: split to do all this.
and this is the output:
g++ -Wall -std=c++98 -I/usr/local/include split.cpp ; ./a.out
[ab] [cde4fg] [hijk lmno] [pqr3stu] [vwx yz ab0cde] [fgh2ij345klm4nop5qr7st]
[ab+*%cde] [fg*hijk lmno%+pqr] [stu--vwx yz ab0cde+fgh] [ij] [] [] [klm] [nop] [qr7st]
[ab+*%cde4fg*hijk] [lmno%+pqr3stu--vwx] [yz] [ab0cde+fgh2ij345klm4nop5qr7st]
ps: i should qualify my first statement; you do not have to make one; unless your idea is to learn
programming.
either the boost string algorithms or the boost tokenizer could be used.
here is an example using the boost string algorithms split. as you can notice, c++ libraries are
far more powerful and expressive. in this example, we are splitting on different criteria and placing the
result into different types of containers. and we are using one function: split to do all this.
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/lambda/lambda.hpp>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
using namespace std;
using namespace boost;
using namespace boost::lambda;
template<typename C> inline void print( const C& cntr )
{ for_each( cntr.begin(), cntr.end(), cout << constant('[') << _1 << "] " ) ; }
int main()
{
string str = "ab+*%cde4fg*hijk lmno%+pqr3stu--vwx yz "
"ab0cde+fgh2ij345klm4nop5qr7st" ;
vector<string> vec ;
split( vec, str, is_any_of("+*%-"), token_compress_on ) ;
print(vec) ; cout << '\n' ;
list<string> lst ;
split( lst, str, is_from_range('2', '6') ) ; // token_compress_off by default
print(lst) ; cout << '\n' ;
deque<string> deq ;
split( deq, str, is_space(), token_compress_on ) ;
print(deq) ; cout << '\n' ;
}g++ -Wall -std=c++98 -I/usr/local/include split.cpp ; ./a.out
[ab] [cde4fg] [hijk lmno] [pqr3stu] [vwx yz ab0cde] [fgh2ij345klm4nop5qr7st]
[ab+*%cde] [fg*hijk lmno%+pqr] [stu--vwx yz ab0cde+fgh] [ij] [] [] [klm] [nop] [qr7st]
[ab+*%cde4fg*hijk] [lmno%+pqr3stu--vwx] [yz] [ab0cde+fgh2ij345klm4nop5qr7st]
ps: i should qualify my first statement; you do not have to make one; unless your idea is to learn
programming.
Last edited by vijayan121 : Apr 14th, 2007 at 4:05 pm.
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 0
I cannot do it myself, because I don't even know anything about (ok ok, i know basics) C++, because I'm a OBJECT programmer... clean C++ is for me ... like a miracle..
#iamthwee
The separator is a char
#iamthwee
The separator is a char
Last edited by MaestroS : Apr 14th, 2007 at 4:31 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- need help in C/PHP (PHP)
Other Threads in the C++ Forum
- Previous Thread: Dynamic Mmeory Allocation with 2D Arrays
- Next Thread: print function not working



Linear Mode