0

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.

0
# include <stdio.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
# include <ctype.h> 
# include <string.h>
# define ROWS 20 
# define COLS 20 

void distintwords(char s[]);
 
int main()
{   
  int i;
  char s[500],x, n;
 
  while(1)
  {
    system("cls");       
    printf("Enter the string: ");
    fgets(s, sizeof(s), stdin); 
    distintwords(s);
    printf("\n\nPress ANY KEY to try again, 'q' to quit : ");
    x = getchar();
    if (x=='q')
      break;
  }
}
 
 
void distintwords(char s[])
{
  int i,j=0,col,row,wordsN=0,pnctsN=0,spacesN=0,breaksN=0; //no k, etc.
  int count, n, outside,len=0,same=1;     //no len or arr[20], added outside
  int lenN[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  char words[ROWS][COLS],*wrd[30],temp[10];

  len = strlen(s)-1;
 
  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])) {     //it's a letter in the range of a-z and A-Z
      words[row][j]=s[i];   
      if(outside) {         //new word
        wordsN++;       
        outside=0;          //not outside any more
      }
      ++j;                  //j is the col of the words[row][col] 
    }
     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;
  } 
  printf("\n\nSTATS:\n");
  printf("\nWORD No.\t\tWORD\t\tCHARS\t\tREPEATED\n");
     
  for(i=0;i <= row ;i++)   // Prints each Word -> for(i=0;i<ROWS;i++)
    {
            if (strlen(words[i])>0)
            printf("\nwords[%d]\t\t%s\t\t%d",i,words[i],strlen(words[i]),lenN[i]);
    }
   
//----------------------STRSWP start-----------------------------
    printf("\n\n");
         for(i=0;i<wordsN;i++)  
            for(j=0;j<wordsN-1;j++)  
                 {  
                    if(strlen(words[j])>strlen(words[j+1]))  
                      {  
                       strcpy(temp,words[j]);  
                       strcpy(words[j],words[j+1]); 
                       strcpy(words[j+1],temp);  
                      }  
                 }  
//----------------------STRSWP END-----------------------------      

//----------------------STRCMP Start---------------------------

      for (i=0,j=0;i<wordsN;i++)
          if (strlen(words[i]) == strlen(words[i+1]))
           {
             if(strcmp(words[i],words[i+1]) == 0)
               { if(same==1)
                   {
                            j++;
                            same=0;
                   }        
                   
                 else
                   {
                      printf("twords[%d]\t\t%s\t\t%d\t\t%d\n",i,words[i],strlen(words[i]),lenN[j]);
                      same=1;
                   } 
              
                } 
             else 
               {
                 lenN[i]=1;  
                 printf("\ntwords[%d]\t\t%s\t\t%d\t\t%d",i,words[i],strlen(words[i]),lenN[j]);
               }
           }
  
//----------------------STRCMP END-----------------------------




//---------------------------------------------------
                 
     for(i=0;i <= row ;i++)   // Prints each Word -> for(i=0;i<ROWS;i++)
    {
            if (strlen(words[i])>0)
            printf("\nwords[%d]\t\t%s\t\t%d\t\t%d",i,words[i],strlen(words[i]),lenN[i]);
    }    
//---------------------------------------------------



//--------------------Print STATS--------------------
  if (len==0 || spacesN==len || pnctsN==len || len==(spacesN+pnctsN))
   {
     //printf("\n\nSTATS:\n");
     printf("\n\nWords        :0");
     printf("\nPunctuations :%d",pnctsN);
     printf("\nBlank Spaces :%d",spacesN);     
     printf("\nCharacters   :%d",len);
   }
 
 else     
   {
     //printf("\n\nSTATS:\n");
     printf("\n\nPunctuations :%d",pnctsN);
     printf("\nBlank Spaces :%d",spacesN);
     printf("\nWords        :%d",wordsN); 
     printf("\nCharacters   :%d",len);
     printf("\n\n NOTE:\n(Characters includes SPACES and Punctuations)");
   }  

}// end of Function distintwords(char s[])

I organized the words according to the Number of characters in descending order :)
The Above code works well except i just cant make this block of code work :(

//----------------------STRCMP Start---------------------------

      for (i=0,j=0;i<wordsN;i++)
          if (strlen(words[i]) == strlen(words[i+1]))
           {
             if(strcmp(words[i],words[i+1]) == 0)
               { if(same==1)
                   {
                            j++;
                            same=0;
                   }        
                   
                 else
                   {
                      printf("twords[%d]\t\t%s\t\t%d\t\t%d\n",i,words[i],strlen(words[i]),lenN[j]);
                      same=1;
                   } 
              
                } 
             else 
               {
                 lenN[i]=1;  
                 printf("\ntwords[%d]\t\t%s\t\t%d\t\t%d",i,words[i],strlen(words[i]),lenN[j]);
               }
           }
  
//----------------------STRCMP END-----------------------------

Well i know the code is not what i wanted it to work like :(
My idea was to count the UNIQUE words :)

1

Then you want to sort the words, alphabetically, not according to the length of the word. We can't have things like:

bat
and
the
cow
the
and

for a sorted word list. We need:

and
and
bat
cow
the
the

and looking at the list, you can perhaps see why.

Now we scan through the word array, and if word[0] is equal to word[1], then that word is not unique. strcmp() will make that comparison for us.

If the word is not repeated (and we again need a flag like outside to keep track of that), then we know that word is unique, in the string.

This is the kind of thing I had in mind:

# 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("\nEnter the string: ");
    //fgets(s, sizeof(s), stdin); 
    printf("\n%s\n", s);
    distintwords(s);

    printf("\n\nPress ENTER to try again, 'q' to quit : ");
    x = getchar();
    if (x=='q')
      break;
  }
  i = getchar(); ++i;
  return 0;
 }
void sort(char words[ROWS][COLS], int row) { 
  int i, j;
  char temp[COLS];

  for(i=0;i<row-1;i++) {
    for(j=i+1;j<row;j++) {
      if(strcmp(words[i], words[j]) > 0) {
        strcpy(temp, words[i]);
        strcpy(words[i], words[j]);
        strcpy(words[j], temp);
      }
    }
  }
  //for(i=0;i<row;i++)     //just a check
    //printf("\n%s", words[i]);

}
void distintwords(char s[])
{
  void sort(char words[ROWS][COLS], int row);

  int i,j,col,row,wordsN=0,pnctsN=0,spacesN=0; //no k, breaksN, 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;
  } 
  sort(words, row);
  for(i=0;i<row;i++)         //shows the sorted words
    printf("\n%s", words[i]);
}

Note that the sort() function is known ONLY to distinctwords(), and nowhere else. It's prototype is in distinctwords(), and the call is made there, also.

This just needs the function to compare one word to the next one, as previously described. Like sort(), it should be prototyped in distinctwords(), and called right at the bottom, after the call to sort().

The trick with strcmp() is the greater than, or lesser than, can be confusing. Remember that capital letters sort out LESS than small letters, 'Z' is less than 'a'.
(that's due to their ascii assigned values). Not all computers use ascii or ASCII, but 99% of the PC's, do use it.

In strcmp(word, word[i+1]). If the words are equal, the return from strcmp() will be 0. Anything else (no matter what value it is), means the words are NOT equal.

That sort is Selection sort, because I memorized that ages ago, and never memorized bubble sort. It's like bubble sort, but slightly shorter and slightly faster.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.