0

I have the following string array:

string userValuesStringStArr[7] = { "Karen", "Tom", "Bob", "Wendy", "Alan", "Ellen", "Kelly" };

I would like to do the following:

  1. Search the entire string and compare all the elements alphabetically.
  2. Based off of those comparisons, create an integer array with their relative values it.
Karen Tom Bob Wendy Alan Ellen Kelly
4 6 2 7 1 3 5

The int array should now look like this:

int arrOrder[7];

The arrOrder values should be: 4 6 2 7 1 3 5

  1. Finally, create a string variable with the values "4, 6, 2, 7, 1, 3, 5" in it.

The idea is that, if I were to switch any of the names at the beginning, it should generate a different integer array, and matching string variable.

For example:

//pre-inserted string in source code

string userValuesStringStArr[7] = { "Karen", "Zoe", "Bob", "Wendy", "Alan", "Ellen", "Kelly" };

//processing happens here

//output

int arrOrder[7]; //4 7 2 6 1 3 5

string arrOrder; //"4, 7, 2, 6, 1, 3, 5"  //the commas and spacing are really important

I know that using c-strings and vectors could help with this problem, but I really need the string array, int array, and string variable to have this format.

I tried using the string compare and assign their relative values that way, but I get stuck in figuring out an algorithm where all string as compared to each other and create integer values.

string userValuesStringStArr[7] = { "Karen", "Tom", "Bob", "Wendy", "Alan", "Ellen", "Kelly" };

int orderStrings[7];
        
for (int count = 0; count < 7; count++)
{
            
    if (userValuesStringStArr[count] < userValuesStringStArr[count + 1])
    {
        orderStrings[count + 1] = count + 1;            
    }
    else
    {
        orderStrings[count] = count + 1;               
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sergio
  • 29
  • 1

0 Answers0