This elminiates the blank line problem:
# include <stdio.h>
# include <stdlib.h> // for "system("cls");" to clear screen
# include <ctype.h> // for isalpha(), ispunt()
#define ROWS 20
#define COLS 20
void distintwords(char s[]);
int main()
{
int i;
char x, n;
char s[500]={"Night is drawing nigh. Shadows of the evening, steal across the sky."};
while(1)
{
//system("cls");
printf("Enter the string: ");
//fgets(s, sizeof(s), stdin);
distintwords(s);
printf("\n\nPress ENTER to try again, 'q' to quit : ");
x = getchar();
if (x=='q')
break;
}
i = getchar(); ++i;
return 0;
}
void distintwords(char s[])
{
int i,j,col,row,wordsN=0,pnctsN=0,spacesN=0,breaksN=0; //no k, etc.
int count, outside; //no len, n, or arr[20],
char words[ROWS][COLS];
for(i=0;i<ROWS;i++) //sets the words[] to empty - a precaution
words[i][0]='\0';
//this loop scans the entire string s, initial state is outside
outside=1; row=i=j=0;
while(s[i]!='\0')
{
if(isalpha(s[i])) {
words[row][j]=s[i];
if(outside) { //new word
wordsN++;
outside=0;
}
++j;
}
else if(s[i]==' ' || ispunct(s[i]) || s[i]=='\n') {
if(!outside) {
words[row][j]='\0'; //add end of string marker
outside=1;
row++;
}
if(s[i] ==' ')
spacesN++;
else if(ispunct(s[i])) //must come after test for space char
pnctsN++;
j=0;
}
++i;
}
for(i=0;i<ROWS;i++)
printf("\n %s ", words[i]);
}
I just put in a fixed string for testing purposes, and added a bit.