I am trying to sort the contents of my array in order but when I compile my code, my program crashes. Anyone see the problem?
Code:
#include<stdio.h>
#include<strings.h>
#include<stdlib.h>
void Swap(char *x, char *y);
void BubbleSort(char *S[], const int size);
struct WordStruct // Structure Definition
{
char Word[51]; // A "Word" is a string that can store up to 50 characters and the null string terminator
int length; // Number of characters stored in the Word (not including null)
} WordList[25]; // Declare an array to store 25 words
int main()
{
int i=0;
char *wordtok;
char InputString[] = "the cat in the hat jumped over the lazy fox done";
wordtok = strtok( InputString, " ");
while(wordtok != NULL)
{
strcpy(WordList[i].Word, wordtok);
printf("%s ", WordList[i].Word);
wordtok = strtok( NULL, " ");
i++;
}
BubbleSort(WordList, i);
return 0;
}
void BubbleSort(char *S[], 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(S[j], S[j + 1]) > 0)
{
/* swap pointers */
tempPtr = S[j];
S[j] = S[j+1];
S[j+1] = tempPtr;
}
}
}
for (j=0; j<=size; j++)
{
printf("%s\n", S[j]);
}
}
void Swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}