(2) Create another cpp source file with name Name Sorting. Write a C++ program to read 5 words of your choice and store them by initializing an array of size 5 called word[]. Write codes to sort these words into alphabetical order. Display the sorted words in a column.
(2) Create another cpp source file with name Name Sorting. Write a C++ program to read 5
words of your choice and store them by initializing an array of size 5 called word[]. Write
codes to sort these words into alphabetical order. Display the sorted words in a column.
Hints to Use: (1) For strings declared using char, data type, the likely functions to
apply are strcpy(variable 1, variable2) for copying from variable2 to
variable1, and strcmp(variable1, variable2) for comparison where results
returned are -1, 0, 1 which are integers. The comparison tests done are
(variable1 == variable2) and (variable1 > variable2). If both variables are
the same strings or same words, a 0 will be returned from the first
comparison. If variable1 is alphabetically on the left and variable2 is on
the right, -1 or a negative integer is returned from the second
comparison. Otherwise, a 1 is returned, denoting that variable2 is greater
than variable 1.
3
General syntax:
strcpy(variable1, variable2);
int cmp = strcmp(variable1, variable2);
(2) For strings declared using string data type, the likely procedures
to apply are (variable 1 = variable2) for copying from variable2 to
variable1, and (variable1 == variable2), (variable1 > variable2) and the
else block all for comparison which results in a true or false.
Besides the above, there is a comparison function called compare() which
does the same thing as strcmp() of activity 2(1) above. This function
returns a –ve, 0, +ve number. The general syntax of this function is
int cmp = declaredStringVariable.compare(variable1, variable);

As per the given information, we need to read 5 words and sort them in alphabetical order. Then print the sorted words in column i.e line by line.
Step by step
Solved in 4 steps with 2 images









