quzah,
I appreciate your help in jogging my thought process. I re-did it using a strcpy and for the moment left the scanf in place. Its not pretty but it works which is all I had to do. Now Im gonna go back and do it the right way. Here is the code I finished with:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N_STRINGS 7
int main(void) //Main program
{
// MAIN VARIABLE INITIALIZATION
int cnt = 0; //Intializes counter for display/accessing the array row
int result; // Gives strcmp a place to put its integer output
int j = N_STRINGS - 1; //Decremental counter and compare variable
char w[N_STRINGS][100]; //Initializes the string array to the number of (N_STRINGS)
char tmp[100]; // Initializes the temp array to swap strings
// MAIN VARIABLES END
//INTRODUCTION
printf("%s %d %s","This program will take", N_STRINGS,"lines of input and re-order them alphabetically\n");
for (cnt = 0; cnt < N_STRINGS; ++cnt) // For statement to fill the array
if (w != '\n') //Continues to fill the buffer until newline is entered from the keyboard
{
//scanf("%s", &tmp2); //New Code
//putchar(w[cnt]); //New Code
printf ("Enter line number %d\n", cnt + 1);
scanf("%s", w[cnt]); //Old Code
}
//Prints each line of the array using the line variable
printf("%s","The lines you entered were:\n");
for (cnt = 0; cnt < N_STRINGS; ++cnt)
printf("w[%d] = %s\n", cnt, w[cnt]);
//COMPARE THE TWO STRINGS
do{
for (cnt = 0; cnt < j; ++cnt)
{
result = strcmp(w[cnt], w[cnt +1]);
//DEBUG PRINTF STATEMENTS TO WATCH LOGIC IN PROGRESS
//printf("The result of %d strcmp is: %d\n", cnt, result);
//printf("This is string 1: %s\n", w[cnt]);
//printf("This is string 2: %s\n", w[cnt + 1]);
//DEBUG PRINTF STATEMENTS END
if (result > 0)
{
strcpy(tmp, w[cnt + 1]);
strcpy(w[cnt+1], w[cnt]);
strcpy(w[cnt], tmp);
}
}
--j;
}while (j > 0);
//Output
printf("%s\n","Here is your sorted output.");
printf("%s\n\n","Strings are sorted alphabetically");
for (cnt = 0; cnt < N_STRINGS; ++cnt)
printf("w[%d] = %s\n", cnt, w[cnt]);
return 0;
}