Answer:
In C++:
void sortlist(char nums[],int charlent){
int Myarr[charlent];
const char s[4] = " ";
char* tok;
tok = strtok(nums, s);
int i = 0;
while (tok != 0) {
int y = atoi(tok);
Myarr[i] = y;
tok = strtok(0, s);
i++;
}
int a;
for (int i = 0; i < charlent; ++i) {
for (int j = i + 1; j < charlent; ++j) {
if (Myarr[i] > Myarr[j]) {
a = Myarr[i];
Myarr[i] = Myarr[j];
Myarr[j] = a;
} } }
for(int j = 0;j<charlent;j++){ printf(" %d",Myarr[j]); }
}
Explanation:
This line defines the sortlist function. It receives chararray and its length as arguments
void sortlist(char nums[],int charlent){
This declares an array
int Myarr[len];
This declares a constant char s and also initializes it to space
const char s[4] = " ";
This declares a token as a char pointer
char* tok;
This line gets the first token from the char
tok = strtok(nums, s);
This initializes variable i to 0
int i = 0;
The following while loop passes converts each token to integer and then passes the tokens to the array
<em> while (tok != 0) { </em>
<em> int y = atoi(tok); </em><em>-> Convert token to integer</em><em>
</em>
<em> Myarr[i] = y; </em><em>-> Pass token to array</em><em>
</em>
<em> tok = strtok(0, s); </em><em>-> Read token</em><em>
</em>
<em> i++;
</em>
<em> } </em>
Next, is to sort the list.
int a;
This iterates through the list
for (int i = 0; i < charlent; ++i) {
This iterates through every other elements of the list
for (int j = i + 1; j < charlent; ++j) {
This condition checks if the current element is greater than next element
if (Myarr[i] > Myarr[j]) {
If true, swap both elements
<em> a = Myarr[i];
</em>
<em> Myarr[i] = Myarr[j];
</em>
<em> Myarr[j] = a;
</em>
} } }
The following iteration prints the sorted array
<em>for(int j = 0;j<charlent;j++){ printf(" %d",Myarr[j]); } </em>
}
<em>See attachment for illustration of how to call the function from main</em>