473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

need algorithm

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

Nov 22 '05 #1
12 1588
jw wrote:
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
Nov 22 '05 #2
jw
but i cant write its code can u help with the code

Nov 22 '05 #3
Ian
jw wrote:
but i cant write its code can u help with the code

Read up on std::string.

Ian
Nov 22 '05 #4
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!
Nov 22 '05 #5
jw wrote:
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
Nov 22 '05 #6
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!
Nov 22 '05 #7
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,

Nov 22 '05 #8

"jw" <ja*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Nov 22 '05 #9
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

Nov 22 '05 #10
* 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;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #11

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


I would like to see you telling that to your manager, few years later...

Nov 22 '05 #12
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);
}
}

Nov 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Nikola | last post by:
I need a function that reads from a txt file and randomly chooses a line from which retrieves a string (with spaces) and returns it to main function. thx
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
8
by: Ben Fidge | last post by:
Hi I'm working on a site which requires the users to specify a hotel at which they're staying in London. The complete list of hotels comes to something like 1600 records. Each record consists of...
10
by: Nemok | last post by:
Hi, I am trying to write an additive encryption algorithm in C++ that will encrypt a text by adding a random numer to each character in a string. The code looks similar to this: for(int...
3
by: Charleees | last post by:
Hi all, I need C# code for Implementing MD5 Algorithm.. Hope all would have heard of MD5 Algorith... Does any one have the C# coding for that Algorithm.. please Send... ITs URgent..... Thanks...
1
by: Charles | last post by:
Hi all, I need C# code for Implementing MD5 Algorithm.. Hope all would have heard of MD5 Algorith... Does any one have the C# coding for that Algorithm.. please Send... ITs URgent..... Thanks...
3
by: Nick Valeontis | last post by:
Hi to all! I am writing an implentation of the a-star algorithm in c#. My message is going to be a little bit long, but this is in order to be as specific as possible. My question has nothing to...
2
nabh4u
by: nabh4u | last post by:
hi, i need some help with progamming..i have a program which has to implement gale shapley's algorithm. i have 2 preference lists one is for companies and the other is for persons. i have to match...
6
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
0
by: chrisotreh | last post by:
hi everyone, i need a simple code of IDA* algorithm. this algorithm is a method of heuristic search.this algorithm is the result of enhancement of Depth First Search combined with A* algorithm.. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.