Sorting a String Array Alphabetically C++ -
i'm trying write program given following structures:
struct aplayer { string name; // name of player int wins; // number of wins player has }; struct acompetition { string name; // name of match int numplayers; // number of players in club aplayer player[10]; // list of players in club };
from there want write function sort players name alphabetically. function declaration follows:
void sortbyname(acompetition & c){}
note: using loops, while loops, , if statement(s). way think compare 2 strings compare ascii values. i'm not sure how input appreciated. thanks!
assuming homework (and if it's not, doing lot more seeing answer,) i'm going give few pointers out.
compare ascii values:
aplayer player1, player2; player1.name = "bill"; player2.name = "john"; if (player1.name[0] < player2.name[0]) { // true, in case, because b less j on ascii table. }
http://www.asciitable.com ascii values. recommend using tolower() on player names, because capital letters lower values lower case letters.
if first digit equal, move on second: (one way of doing this.)
aplayer player1, player2; player1.name = "alfred"; player2.name = "alvin"; // find name shorter using .length() player2.name.length() // loop through next part aplayers in acompetition (int = 0; < shortername.length(); i++) { // compare ascii values showed above. // if 1 larger other, swap them. }
Comments
Post a Comment