need algorithm

J

jw

i have an array called program;
char program[500];
it includes for ex. this=> mp3#name#surname#age#sex#school#

there are some names in the array; there may be names more than 6,but
not totaly longer than 500 chars.
i want to make this in a loop
cout<<name[];
i ll use an array called name with size 20;and the output will be
mp3
name
surname
 
J

John Harrison

jw said:
i have an array called program;
char program[500];
it includes for ex. this=> mp3#name#surname#age#sex#school#

there are some names in the array; there may be names more than 6,but
not totaly longer than 500 chars.
i want to make this in a loop
cout<<name[];
i ll use an array called name with size 20;and the output will be
mp3
name
surname

Here's the algorithm

find the position of the first # in program
copy all the character from the beginning of program upto the first #
output all the copied characters
find the position of the second # in program
copy all the character from after the first # upto the second #
output all the copied characters
find the position of the third # in program
copy all the character from after the second # upto the third #
output all the copied characters

I don't quite get what you are saying about loops, but whatever it is
put the above into the loop you need.

john
 
S

sisma

jw ha scritto:
but i cant write its code can u help with the code

what about this:

#include <iostream>
#include <cstring>

int main(){
char program[500] = "mp3#name#surname#age#sex#school#";
char *tokenPtr;

tokenPtr = strtok(buffer, "#");
while(tokenPtr){
std::cout << tokenPtr << std::endl;
tokenPtr = strtok(NULL, "#");
}

return 0;
}

bye!
 
J

John Harrison

jw said:
but i cant write its code can u help with the code

You should try, that is how you learn. For instance finding the position
if the first # in a string is a simple loop. It is about three or four
lines of code. If you really cannot even try that then you need to do
something other than programming.

Have a go and if you get stuck post the code here.

john
 
S

sisma

jw ha scritto:
but i cant write its code can u help with the code

what about this:

#include <iostream>
#include <cstring> // maybe unnecessary

int main(){
char program[500] = "mp3#name#surname#age#sex#school#";
char *tokenPtr;

tokenPtr = strtok(program, "#");
while(tokenPtr){
std::cout << tokenPtr << std::endl;
tokenPtr = strtok(NULL, "#");
}

return 0;
}

bye!
 
J

jw

i tried and i did without using "strtok",but i only wondered if there
is a function which ll do what i want now i learned the "strtok"
function,thanks all,
 
M

Mike Wahler

jw said:
i tried and i did

If you tried, then you have code. Show us your code.
without using "strtok",but i only wondered if there
is a function which ll do what i want now i learned the "strtok"
function,thanks all,

IMO 'strok()' is not a good way to solve this simple
task (for one thing it disallows the possibility of
const input).

-Mike
 
J

jw

i did it without using strtok but i wondered if there is a function
which ll do it,now i learned how to use strtok thanks all
 
A

Alf P. Steinbach

* jw:
i have an array called program;
char program[500];
it includes for ex. this=> mp3#name#surname#age#sex#school#

there are some names in the array; there may be names more than 6,but
not totaly longer than 500 chars.

Forget arrays: don't use a lower level than you have to.
i want to make this in a loop

Forget arrays: don't use a lower level than you have to.
cout<<name[];
i ll use an array called name with size 20;and the output will be
mp3
name
surname

void display( std::string const& mp3Data )
{
std::string data = mp3Data;
std::replace( data.begin(), data.end(), '#', '\n' );
std::cout << data;
}
 
F

fastback66

void Tokenize ( const string& str, vector<string>& tokens,
const string& delimiters = " ");

// stores all words of a string in a vector
void Tokenize( const string& str,
vector<string>& tokens,
const string& delimiters )
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);

// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);

while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));

// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);

// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top