Originally Posted by
itCbitC
Can you show what the code for the next part of the problem looks like?
Why not?
The next part of the problem involved taking the newly "parsed" array of words, sorting them in alphabetical order and then printing them.
Code:
//prototypes
void ParseSentence(char *ptrA);
void BubbleSort(char *ptrA[], const int size);
void PrintList(char *ptrA[], const int size);
//call in main
ParseSentence(Buffer3);
//Break a sentence up into words, sort them alphabetically and print them
//Input: a NULL terminated string
//Output:a printed list of the words in the string sorted alphabetically
void ParseSentence(char *ptrA)
{
char temp[80];
int i, j;
int count = 1;
strcpy(temp, ptrA);
int size = strlen(temp);
char *words[80];
words[0] = strtok(temp, " .,;");
printf("%s\n", words[0]);
for(i = 1; i < size - 1; i++)
{
words[i] = strtok(NULL, " .,;");
if(words[i] == NULL)
{
break;
}
count++;
}
printf("Buffer3 in alphabetical order(Capital letters have priority):\n\n");
BubbleSort(words, count);
PrintList(words, count);
}
//Now that we have an array of words, we need to sort them alphabetically
//Input: an array or words and it's size
//Output: the words in the array sorted alphabetically
void BubbleSort(char *ptrA[], const int size)
{
int pass;
int j;
char *tempPtr;
for (pass=0; pass < size - 1; pass++)
{
for (j=0; j<size - 1; j++)
{
if (strcmp(ptrA[j], ptrA[j + 1]) > 0)
{
/* swap pointers */
tempPtr = ptrA[j];
ptrA[j] = ptrA[j+1];
ptrA[j+1] = tempPtr;
}
}
}
}
//Once the list of words is sorted, we need to print it
//Input: the array of words to be printed and it's size
//Output:
void PrintList(char *ptrA[], const int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%s\n", ptrA[i]);
}
}