Question: Consider the following C + + program: #include #include #include using namespace std; void bubble ( int , char * [ ] ) ; void

Consider the following C++ program:
#include
#include
#include
using namespace std;
void bubble(int, char*[]);
void swap(char *[], int, int);
int main()
{
int size =10;
int count;
char *array[size];
char str[]= "this is the ten token sentence that should be sorted";
char *tokenptr;
tokenptr = strtok(str,"");
count =0;
while (tokenptr != nullptr)
{
array[count]= tokenptr;
tokenptr = strtok(nullptr,"");
count++;
}
for (int i =1; i <15; i++)
{
cout << array[i]<< endl;
}
bubble(size, array);
cout << "sorted list:" << endl;
for (int i =1; i <15; i++)
{
cout << setw(10)<< array[i]<<" length is "<< strlen(array[i]);
cout << endl;
}
return 0;
}
void bubble(int size, char *arr[])
{
for (int j =1; j < size; j++)
{
for (int count =0; count < size -1; count++)
{
if (strcmp(arr[count], arr[count +1])>0)
{
CSCE 1045 Lab 10
4
swap(arr, count, count +1);
}
}
}
return;
}
void swap(char *ptr1[], int first, int second)
{
char *temp;
temp = ptr1[first];
ptr1[first]= ptr1[second];
ptr1[second]= temp;
return;
}
This program uses a bubble sort to sort a list of ten words. The complexity of the
program is to break the character string into "tokens". Since this program uses the
strtok() function to do this, you may want to search www.cplusplus.com or look
at the man page for strtok (e.g., man strtok). Please note that you will not be
tested over this function, but it may help you as you proceed in your academic and
professional career

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!