All I want to do is ask the user to enter words, then I bubble sort them alphabetically, then print them back out. THE PROBLEM IS.. When I try to store "str" into the words array, idk what's happening... When I print them out, it's only saving the LAST word I typed into every array location..
Code:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#define MAXLENGTH 15
#define MAXWORDS 3
int _tmain(int argc, _TCHAR* argv[])
{
char str[80];
char *words[25];
char *hold;
int j;
int i;
int wordcount=0;
while (wordcount<MAXWORDS){
printf("Enter a word: ");
fgets(str, MAXLENGTH, stdin);
/* remove newline, if present */
i = strlen(str)-1;
if( str[i] == '\n')
str[i] = '\0';
printf("%s\n",str);
words[wordcount]=str;
wordcount++;
}
printf("%d words.\n",wordcount);
for(i=1;i<wordcount;++i)
for(j=wordcount-1;j>=i;--j)
if (strcmp(words[j-1],words[j])>0){
hold=words[j-1];
words[j-1]=words[j];
words[j]=hold;
}
for(i=0;i<wordcount;i++)
printf("%s \n",words[i]);
return 0;
}
I realize "char*s are not strings. They are pointers"
But how do I fix this?
All I want to do is ask the user to enter strings 1 line at a time, and have them saved in an array.
Then sort the strings alphabetically in the array.
Then print it.
That's all!!.. I have the sorting working, I just don't have the storing the strings from the user into the array!