problems with Sorting strings/arrays in a user defined lexical order.

I have this problem, and i need some help and instructions. I previously tried to put strings in an array, but i couldn't modify the first index in the string that is in the array. I changed my code later.
url:http://www.cs.rit.edu/~icpc/questions/2010/WNEC_2010.pdf

EDIT: Anyone can suggest a function that sorts the inputs vertically according to the order given by the user? I made this one

void order(string order, char index[][15]){
int counter=0,line=0,i=0,j=0;
for(i=0;i<=15-1;i++)
{for(j=0;j<=15-1;j++)
{
if(index[j][i]==order[counter])
{ for (int b=0;b<=15-1;b++)
index[i][b]=index[j][i];
continue;}
else continue;}

counter++;
}

Sample Input
C M H B F
hccbmf
mhc
hccfc
cfh
hbcf
mbc
@
Sample Output
Cfh
Mhc
Mbc
Hccbmf
Hccfc
Hbcf
"

My current-unfinished code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
using namespace std;
void capital(char[][15]);
void read(char[][15], string);
void order(string, char[][15]);
void print(char[][15]);

int main(){
	char index [50][15];
	string order;

	read(index,order);
	capital(index);
    //order(order,index);
	print(index);

	
	
	

	
	

	system("pause");

		return 0;
	}

void capital(char index[][15]){
	for(int i=0;i<=50-1;i++)
		
			index[0][i]-=32;
}

void read(char index[][15], string order){
	cin>>order;
	int i=0,j=0;
	for(i=0;i<=50-1 && index[i][j]!='@';i++)
		for( j=0;j<=15-1 && index[i][j]!='@';j++)
			cin>>index[i][j];}

void order(string order, char index[][15]){
	int counter=0,line=0,i=0,j=0;
	for(i=0;i<=15-1;i++)
	{for(j=0;j<=15-1;j++)
	{ 
		if(index[j][i]==order[counter])
	   { for (int b=0;b<=15-1;b++)
			index[i][b]=index[j][i];
		continue;}
		else continue;} 

	counter++;
	}
	    
}

void print(char index[][15]){
	for(int i=0;i<=50-1;i++)
		for(int j=0;j<=15-1 || index[i][j]!='@';j++)
			cout<<index[i][j];
}

	
Last edited on
Please use code tags next time. They look like <> to your right.
Maybe this is what you're looking for. The standard library provides the sort function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <vector>
#include <algorithm>


int main()
{
    std::vector<std::string> items
    {
        "C M H B F",
        "hccbmf"
        "mhc",
        "hccfc",
        "cfh",
        "hbcf",
        "mbc"
    };

    std::sort(items.begin(), items.end());

    for (std::vector<std::string>::iterator it = items.begin(); it != items.end(); ++it)
        std::cout << *it << std::endl;

    return 0;
}
Topic archived. No new replies allowed.