# Sorting arrays

• 05-23-2002
Unregistered
Sorting arrays
Hello, I need help. How would I go about doing this:
Getting character string from user - store into an array..user then inputs order that the character string to be printed out to screen. Any ideas?
For example:
user puts in: e l l o H
user then puts in: 2 3 4 5 1
Outputs to screen: Hello

> i think it requires two arrays and a recursive sort dont know to go about it an example would be great<

thanks a lot
• 05-23-2002
Azuth
This would be a crude answer I know. but you could;
Read the string into an array 'mytext[]', then read the users number input as a string, tokenise it with spaces or comma's.
2,3,1,5,4. use atoi, to turn them into ints.
Giving you
a=2
b=3
c=1
d=5
e=4
etc.
Then just print from your array with something like
cout << mytext[a] << mytext[b] etc

I imagine this would work, but I'm sure there's something far more dynamic that could be done (I'm just another newb, and I don't write good answers when I'm away from my compiler)
• 06-19-2002
fero45
oleHl - Hello
This should solve the problem of a word of any length.
#include <iostream>
#include <stdlib.h>
using namespace std;

void main() {
char word[10];
int numbers[10];
int i, length, a=1, b;

cout << "Enter a word: "; // oleHl - which gives "Hello"
cin >> word;

// Let's get the number of letters in our word:
length = strlen(word); // 5 letter
//Now, enter the order of letters- 5,4,2,1,3

cout << "Enter the order of " << length << " letters: ";

for(i=0; i<length; i++)
cin >> numbers[i];

for(b=0; b< length; b++)
for(i=0; i<length; i++)
{
if(numbers[i] == length-(length-a)) {
cout << word[i];
a++; }
else
continue;
}
cout << endl;
}
• 06-19-2002
fero45
oleHl - Hello
This should solve the problem of a word of any length.
#include <iostream>
#include <stdlib.h>
using namespace std;

void main() {
char word[10];
int numbers[10];
int i, length, a=1, b;

cout << "Enter a word: "; // oleHl - which gives "Hello"
cin >> word;

// Let's get the number of letters in our word:
length = strlen(word); // 5 letter
//Now, enter the order of letters- 5,4,2,1,3

cout << "Enter the order of " << length << " letters: ";

// we take each number at a time after hitting the RETURN key
for(i=0; i<length; i++)
cin >> numbers[i];

for(b=0; b< length; b++)
for(i=0; i<length; i++)
{
if(numbers[i] == length-(length-a)) {
cout << word[i];
a++; }
else
continue;
}
cout << endl;
}