Sir, i'm new here searched the forums and web.
I'm a beginner and dont know C very well.

The Question actually is this:

Write a C function called DistinctWords that counts the distinct words in its input string and prints them in the descending order of number of its occurrence. Precede each word by its count. Write a main function to test your code with different input values to demonstrate that your function is robust.

The function signature in C is as follows:

void DistinctWords(char s[]);

Thought strtok() might help and wrote this coe:

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen

 void distintwords(char s[]);
 int main()
 {   
     while(1)
     {
     system("cls");       
     char s[1000],x;
     printf("Enter the string: \n");
     gets(s);
     distintwords(s);
     printf("\n\nPress ENTER to try again, 'q' to quit : ");
     x = getch();
     if (x=='q')
     break;
     }
 }

void distintwords(char s[])
{
     int slen,i,j,k;
     char * tok;
     char * toks[20];
     slen == strlen(s);
     tok = strtok (s," ,.-?!");
     while (tok != NULL)
     {
      printf ("%s\n",tok);
      tok = strtok (NULL," ,.-?!");
      for(i=1;i<10;i++)
        {
           toks[i]=tok;
           printf ("Test: %s\n",tok[i]);
        }  
                    
     }
}

The above code i'm not knowing how to allot each word to different variable like tok[1],tok[2], etc so i can compare.

The other code tried by my friend is:

# include <stdio.h>
# include <conio.h>
#include<string.h>
 void distintwords(char s[]);
 int main()
 {
     char s[100];
     
     
     printf("enter the string");
     gets(s);
     distintwords(s);
     getch();
 }

 void distintwords(char s[])
 {
    int arr[20],p1=0,p2=0,count=1,n;     
     char str[20][20];
     int i=0,k=0,v=0;

  for(i=0;s[i]!='\0';i++)
{
        k=0;
           static int j=0;

        for(;s[j+i]!='\0'&& s[j+i]!=' ';j++)

        {

            str[v][k]=s[j+i];

                k++;

        }

        str[v][k]='\0';

            v++;

  }
       int words=0;

       int c=0;

while(s[c]!='\0')

{

if(s[c]==' ')

words++;

c++;

}

printf("%d\n",words+1);

     for(i=0;i<words;i++)
     {
       arr[i]= strlen(str[i]);
     }
  for(i=0;i<words;i++)
  {
      for(k=0;k<words;k++)
     
     {
       if(arr[i]==arr[k])
               n= strcmp(str[i],str[k]);
            if(n==0) 
            { 
                count++;
              printf("%d   %s",count,str[i]);
            }
                
                          
    else {
        for(i=0;i<=words;i++)
         printf("\n %d  %s",count,str[i]);
        }
      }
   
   }
 }

Both outputs are very annoying.


So for now i'm left with this code:

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen

 void distintwords(char s[]);
 int main()
 {   
     while(1)
     {
     system("cls");       
     char s[1000],x;
     printf("Enter the string: \n");
     gets(s);
     distintwords(s);
     printf("\n\nPress ENTER to try again, 'q' to quit : ");
     x = getch();
     if (x=='q')
     break;
     }
 }

void distintwords(char s[])
{
     int slen,i,j,k;
     char * tok;
     char * toks[20];
     slen == strlen(s);
     tok = strtok (s," ,.-?!");
     while (tok != NULL)
     {
      printf ("%s\n",tok);
      tok = strtok (NULL," ,.-?!");
      for(i=1;i<10;i++)
        {
           toks[i]=tok;
           printf ("Test: %s\n",tok[i]);
        }  
                    
     }
} 
     
     /*
     int arr[20],p1=0,p2=0,count=1,n;     
     char str[20][20];
     int i=0,k=0,v=0;

  for(i=0;s[i]!='\0';i++)
   {
     k=0;
     static int j=0;

        for(  ;s[j+i]!='\0'&& s[j+i]!=' ';j++)
        {
            str[v][k]=s[j+i];
            k++;
        }
        
        str[v][k]='\0';
        v++;
   }
   
int words=0;
int c=0;

while(s[c]!='\0')
{
  if(s[c]==' ')
  words++;
c++;
}   
printf("Total Number of Words: %d \n \n",words+1);

  for(i=0;i<words;i++)
     arr[i]= strlen(str[i]);
        
  for(i=0;i<words;i++)
    for(k=0;k<words;k++)
       if(arr[i]==arr[k])
         {
           n = strcmp(str[i],str[k]);
               if(n==0) 
                 { count++;
                   printf("%s : %d \n",str[i],count);
                 }
                
                else 
                 {
                   //for(i=0;i<=words;i++)
                   printf("%s : %d \n",str[k],count-1);
                 }
          }
}
*/

Sorry for the mess.

For me, the above just won't do. I couldn't care less about the "annoying" aspect of the output!

***WHAT*** is the problem that has you stumped --- EXACTLY.

What is your input - how many words do you need to handle. What is your output - what's wrong with it that you can't figure out.

No way am I going to fix some program for your assignment, because it annoys you.
;)

Welcome to the forum, btw. Now get your problem solving hat firmly in place, and get into this assignment's details.

well its not my assignment actually and i dont think you might believe it. its my friends assignment and i'm trying to learn parallely. from their assignments.

Now about the code:

First code makes no sense i mean this code:

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
 
 void distintwords(char s[]);
 int main()
 {   
     while(1)
     {
     system("cls");       
     char s[1000],x;
     printf("Enter the string: \n");
     gets(s);
     distintwords(s);
     printf("\n\nPress ENTER to try again, 'q' to quit : ");
     x = getch();
     if (x=='q')
     break;
     }
 }
 
void distintwords(char s[])
{
     int i;
     char * tok;
     char * toks[20];
     slen == strlen(s);
     tok = strtok (s," ,.-?!");
     while (tok != NULL)
     {
      printf ("%s\n",tok);
      tok = strtok (NULL," ,.-?!");
      printf ("%s\n",tok[i]);
      }
}

the above code works like this:

iput: "hi how are you"
output is:
hi
how
are
you

Thats it :(

then i added this part of code:

for(i=1;i<10;i++)
        {
           toks[i]=tok;
           printf ("Test: %s\n",tok[i]);
        }

thinking that each word might be alloted to each string variable, but its not whats happening :(

The output window is freezing :(

i'm using DEV C++ compiler on windows Seven.

A friend of mine suggested this example

for (int i = 0; i == strlen(s); i++)
    {
     if ( == '.' || string.charAt(i) == ':' || string.charAt(i) == ';' )
{

numberofWords += 1;

}
}
System.out.println(numberofwords);

Its JAVA ( i guess ) i'm so bad with C and i cant easily get through with JAVA. But this thing:

if ( == '.' || string.charAt(i) == ':' || string.charAt(i) == ';' )

Seems interesting.


I'm making this whole this soo messy, sorry.

Can you Help me ???

:(

Sure, but the first thing I want you to do, is to throw your friends code, away. :)

This is an exercise to develop your ability to scan a char array and use logic on it. Forget strtok(), (it's for later), and let's get into top down design:

1) Analyze the problem by doing it by hand (in this case), and jot down the essential steps you use over and over, for each word.

2) Write down those steps, in the order you do them, in small steps of logic, in english.

3) Now, we'll take those steps, and write code to match. Putting off all the non-essential details, like output formatting, etc.

4) Once the essential parts are in place, (functions, and logic), we'll add in the other details, we had put off, previously.

You can't write a program to solve a problem, that you don't know how to solve, yourself. You have to know the steps, and the order of those steps.

Here, you have two states: outside a word, and inside a word, as you scan each char. Spaces and punctuation char's change that state from inside to outside.

You can't learn to swim by standing on the shore, watching others. Forget what the codes of others may do/not do, and let's get YOU into the water.

Don't worry - I'm a Water Safety Instructor. ;)

Well the strtok() prog was my idea :(

I just can only think of reading a string by gets()

Counting the length by strlen()

And Counting the words bu counting the SPACE or Punctuations

I cant get any further .

And talking about the water how did you know i'm afraid of water

DELETED

Reason: understood the mistake but still stick with the code :(

gets() is VERY unsafe, since it has the defect of allowing the user to overfill the buffer (potentially crashing the whole program or even taking control of the machine it's running on).

Use fgets(). It works on ANY stream, (file, keyboard, etc.), allows you to limit the amount that will go into a char array, and makes you float better -- maybe! ;)

Think about it, and then write down your steps - how would you count words in text (a string)? Sounds like you have the idea. Would you do it somehow differently for every word, or could you use a loop, and do each word in the same way, using the loop?

What variables would you need? Maybe you can see what I'm getting to here. strtok() is a "water wing" crutch, for this exercise. Save it for later.

I can smell fear right over the internet. ;)

I remember once Lifeguarding for a mile swim, at a pool. A guy in leg braces and using heavy metal arm "crutches", wanted to sign up for the swim.

"Oh", he said "don't worry, I don't use these when I'm in the pool". That was great, because I couldn't let him in with them on - he'd sink like a rock.

"I can swim a mile, don't worry", he added. "I just get horizontal, and propel myself with my arms, and slightly plane up and down to breathe."

I let him swim, and he did OK. I found out later he swims the mile there, every year.

You can do this. Just get your head into it, and follow the steps I posted, earlier - and get used to using fgets(). It's the way you want to go, believe me.

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
 
 void distintwords(char s[]);
 int main()
 {   
     while(1)
     {
     system("cls");       
     char s[500],x;
     printf("Enter the string: \n");
     fgets(s, sizeof(s), stdin); 
     distintwords(s);
     printf("\n\n\nPress ENTER to try again, 'q' to quit : ");
     x = getch();
     if (x=='q')
     break;
     }
 }
 
void distintwords(char s[])
{
   int i=0,j,k,len,words=0;
   int wrd[20];
   char * str;
   len = strlen(s);
 while(s[i]!='\0')
   {
     if(s[i]==' ') 
     words++;
     i++;
   }
   
 if (len==0)
   {
     printf("\n\nSTATS:\n\nWords: %d",words);     
     printf("\nSTATS:\nCharacters:%d \n",len);
   }
 
 else 
   {
     printf("\n\nSTATS:\n\nWords: %d",words+1); 
     printf("\nCharacters:%d \n",len-1);
   }
}

I was just posting the above code and then read your valuable suggestions.
The above code is reading the chars and words fine. I mean its a very small step ahead.


I looked for fgets();
http://beej.us/guide/bgc/output/html/multipage/gets.html

WOW that was great. Thank You. i changed the gets(); to fgets(); :)

The fgets(); is adding an extra line so i just used

len-1

but the its suggested to use this code

char *remove_newline(char *s)
{
    int len = strlen(s);

    if (len > 0 && s[len-1] == '\n')  // if there's a newline
        s[len-1] = '\0';          // truncate the string

    return s;
}

Is it ok for now ?

I can feel flapping in water but cant move :(

I still cant get the UNIQUE word counting.
I can think of "strcmp();"

P.S: My English is bad, Excuse

Wow! That was great timing.

Your English is fine, no worries.

fgets adds on (if space is available), an end of string char: '\0', to the string. (Because a bunch of char's are just a bunch of char's, without an end of string marker to mark them as a string.)

gets() doesn't include the newline: '\n' char that is generated by the user whenever the enter key is pressed, into the saved string. fgets() does include it, always.

So you may have TWO unseen char's on the end of your fgets() string: a newline and an end of string char. You ALWAYS want that string buffer to be generously bigger than any line of char's you want to save as a string with fgets().

To find the unique words, we need to save each word, and compare it with all the other words. So we'll need wrds[] to become wrds[][20]. That is, it needs to be a 2 dimension char array.

How many words are you going to have to handle at any time, looking for unique words? We need a size for the first dimension of wrds[SOME_SIZE][20].

Then we can find unique words using strcmp(), after we save each word into the wrds[][] array.

so does "SOME_SIZE" here refer to the length of each word ? :icon_rolleyes:

No. It refers to the number of rows (number of words), you want to process, in total.

SOME_SIZE is not the best name for it, but it IS the size for the first dimension of the wrds[][] array.

oh ok.

So its not necessary for me to be using a particular number right ?
i mean for the time, can give some 20.

s = "hello, how are you? hello !"

so this should become like

s1= "hello"
s2="how"
s3="are"
s4="you"
etc

then i can use:

strlen(); and strcmp();

To work around. but just the thing, how do i divide the sentence :( in to word ???

2D Array :(

You can have an array that is FIXED in size, and declared looking like an array:

char array;

Or you can have an array that is FIXED in size, but declared looking like a block of memory arranged for an array:

char *array;

The latter one is generally used either:

1) To be cute and smart looking

2) Because the array size is unknown at this time, and the array memory will
be dynamically malloc'd later on.

The key thing is, "under the hood", arrays in C are really blocks of memory, arranged in a particular way. However, it's hard to work with arrays as "blocks of memory...". Non programmers won't lightly tolerate that kind of crap, and you shouldn't work with it that way, unless you're really comfortable with it.

One key concept in programming is to keep your code and logic CLEAR and SIMPLE. If you write your code "oh so cleverly", you (and others), will find it a witch to de-bug, update, or extend the program, later.

<< BE AS CLEAR AS YOU REASONABLY CAN IN YOUR CODE >>

For this program, we need to give it a size, but here's a help:

#define ROWS 100
#define COLS 20

Note, no semi-colons at the end of either line of code!

Put that right under your include lines of code, and then use them to make up your wrds[ROWS][COLS], array.

Any changes can be made by changing just one value, in one place.

You already have divided the sentence into each word! When you counted it, remember?

When you find the first letter of a word, you start putting all letters into a row of the wrds array. Keep going until you hit the next space or punctuation char.

Then go to the next row. :)

You'll want a loop for that:

for(i=0;i<ROWS;i++)
  if(word[i] && OUTSIDE) { //add && !punctuation to this
    //it's the start of a new word
    OUTSIDE= 0;
    col=0; //row value is carried into this loop
    wrds[row][col]= word[i];
    col++;     
  }
  else if( //code to handle char's when you are already inside a word ) {
    //col variable is not reset to zero
    wrds[row][col]= word[i]; 
    col++;
    
  }
  //etc.

That kind of thing. Have an errand or two to run, back in 2 hours.

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
 
 void distintwords(char s[]);
 int main()
 {   
     while(1)
     {
     system("cls");       
     char s[500],x;
     printf("Enter the string: ");
     fgets(s, sizeof(s), stdin); 
     distintwords(s);
     printf("\n\nPress ENTER to try again, 'q' to quit : ");
     x = getch();
     if (x=='q')
     break;
     }
 }
 
void distintwords(char s[])
{
   int i=0,j=0,k=0,c=0,R=0,len=0,words=0,pncts=0,spac=0,breaks=0;
   len = strlen(s)-1;
   
   while(s[i]!='\0')
   {
     if(s[i]==' ') 
     words++;
     i++;
   }
   
 while(s[j]!='\0')
   {
     if( s[j]==',' || s[j]=='.' || s[j]==';' || s[j]=='?' || s[j]=='!' || s[j]==':' ) 
     pncts++;
     j++;
   }
 spac=words; 
  //----------------------------------------------------------
    int arr[20],count=1,n;     
    char str1[20][20];

  for(i=0;s[i]!='\0';i++)
     {
        c=0;j=0;
        for( ;s[j+i]!='\0' && s[j+i]!=' ';j++)
         {
            str1[R][c]=s[j+i];
            c++;
         }
        str1[R][c]='\0';
        R++;
    }
  //----------------------------------------------------------
    for(i=0;i<=words;i++)
       arr[i]= strlen(str1[i]);
    
    for(i=0;i<words;i++)
     {
       for(c=0;c<words;c++)
         { arr[c]=strlen(str1[c]);
          if(arr[i]==arr[c])
             n= strcmp(str1[i],str1[c]);
           if(n==0) 
             { 
               count++;
               printf("%d   %s\n",count,str1[i]);
             }
                        
           else 
           {
            for(i=0;i<=words;i++)
            printf("\n %d  %s",count,str1[i]);
           }
    }
   
  }
breaks=spac+pncts;
//test
for(i=0;i<=breaks;i++)
printf("\narr[%d] = %d",i,arr[i]);

  //----------------------------------------------------------

 if (len==0 || spac==len || pncts==len || len==(spac+pncts))
   {
     printf("\n\nSTATS:\n");
     printf("\nWords        :0");
     printf("\nPunctuations :%d",pncts);
     printf("\nBlank Spaces :%d",spac);     
     printf("\nCharacters   :%d",len);
   }
 
 else 
   {
     printf("\n\nSTATS:");
     printf("\nPunctuations :%d",pncts);
     printf("\nBlank Spaces :%d",spac);
     printf("\nWords        :%d",words+1); 
     printf("\nCharacters   :%d",len);
     printf("\n\n NOTE:\n(Characters includes SPACES and Punctuations)");
   }
}

Its all gone wrong :(

for(i=0;i<spac;i++)
  if(s[i] && " ") 
    {
    outside= 0;
    col=0; 
    wrds[row][col]= word[i];
    col++; 
    }
  
  else 
    {
        wrds[row][col]= word[i]; 
        col++;
    }
 for(i=0;i<=col;i++) 
 printf("\n %d : %s",i,word[i]);

When ever i see a SPACE i need to start saving the characters to a new colum.
But what is OUTSIDE ??

Please help :(

outside is one of the two states your program can be in, as it scans through the text. Just a "flag", and doesn't need to be capitalized.

When you reach a space (or punctuation mark), you should start saving to a new row, not a new column.

I'll study your code. Watching the Tour de France, just now though.

thought of "strncpy" but it think its not possible either .

Ok have a good time :)

Tough luck in the Tour - leaders chain came off at a critical time, and he lost his lead. He's just second in the race, now.

This is what I need in formatting to help with your program. Note that ALL the variables have to be declared at the top of the function - no exceptions.

In general, you had some curly braces { without a matching closing brace. For understanding your code, make sure that the closing brace in the pair, is DIRECTLY BELOW either the opening brace, or the first letter of the line that starts that block of code.

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
 
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 ENTER to try again, 'q' to quit : ");
    x = getch();
    if (x=='q')
      break;
  }
  i = getchar(); ++i;
  return 0;
 }
 
void distintwords(char s[])
{
  int i=0,j=0,k=0,c=0,R=0,len=0,words=0,pncts=0,spac=0,breaks=0;
  int arr[20],count=1,n;     
  char str1[20][20];
  len = strlen(s)-1;
   
  
//This is not complete. It doesn't account for sentences like this,
//where the line ends with a comma, a period, etc., instead of a space.

  while(s[i]!='\0')
   {
     if(s[i]==' ') 
     words++;
     i++;
   }
   
 while(s[j]!='\0')
   {
     if( s[j]==',' || s[j]=='.' || s[j]==';' || s[j]=='?' || s[j]=='!' || s[j]==':' ) 
     pncts++;
     j++;
   }
 spac=words;  //why would spaces equal words?
  //----------------------------------------------------------
    
//initialize R to zero, inside the for loop assignments:
  for(i=0, R=0; s[i]!='\0';i++)


/* We know that words don't end with just spaces, so this can't be right */
  for(i=0;s[i]!='\0';i++)  
     {
        c=0;j=0;
        for( ;s[j+i]!='\0' && s[j+i]!=' ';j++)
         {
            str1[R][c]=s[j+i];
            c++;
         }
        str1[R][c]='\0';
        R++;
    }
  //----------------------------------------------------------
    for(i=0;i<=words;i++)
       arr[i]= strlen(str1[i]);
    
    for(i=0;i<words;i++)
    {
      for(c=0;c<words;c++)
      { arr[c]=strlen(str1[c]);
        if(arr[i]==arr[c]) {
          n= strcmp(str1[i],str1[c]);
          if(n==0) 
          { 
            count++;
            printf("%d   %s\n",count,str1[i]);
          }
          else 
          {
            for(i=0;i<=words;i++)
              printf("\n %d  %s",count,str1[i]);
          }
        }
      }
    }

breaks=spac+pncts;   //this is out of place, in it's formatting
//test
for(i=0;i<=breaks;i++)
printf("\narr[%d] = %d",i,arr[i]);

  //----------------------------------------------------------

 if (len==0 || spac==len || pncts==len || len==(spac+pncts))
   {
     printf("\n\nSTATS:\n");
     printf("\nWords        :0");
     printf("\nPunctuations :%d",pncts);
     printf("\nBlank Spaces :%d",spac);     
     printf("\nCharacters   :%d",len);
   }
 
 else     //this is great style
   {
     printf("\n\nSTATS:");
     printf("\nPunctuations :%d",pncts);
     printf("\nBlank Spaces :%d",spac);
     printf("\nWords        :%d",words+1); 
     printf("\nCharacters   :%d",len);
     printf("\n\n NOTE:\n(Characters includes SPACES and Punctuations)");
   }
}

Don't worry about the word comparisons, right now. Get the words counted correctly first. Then get the words put into the array[][], and checked for accuracy. THEN we'll worry about getting the other stuff (test for uniqueness, etc.), done with the words.

I've made some changes to the above code, so it's not just formatted differently. This is the only version I can compile, and test. Please work from here, forward.

Let's look at some logic for distintwords():

here, I've added
#define ROWS 20 //and
#define COLS 20 //as well, right below the include files.

I removed conio.h, string.h, and added #include <ctype.h>. Removed some other variables, as well. notes in the code for most of it.

str1[][] I renamed words[][], because variable names beyond k that have no particular
meaning to me, drive me bonkers.


an N appended to the end of a variable name, means it's a Number. row and col inside the function, are not related at all, to ROWS or COLS, of course.

void distintwords(char s[])
{
  int i,j,col,row,wordsN=0,pnctsN=0,spacesN=0,breaksN=0; //no k, etc.
  int count, n, outside;     //no len or arr[20], added outside
  char words[ROWS][COLS];
                             //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') { //end of a word
      words[row][j]='\0';      //add end of string marker 
      outside=1;               //we're outside 
      if(s[i] ==' ')
        spacesN++;             //count it, if it's a space
      else if(ispunct(s[i]))   //must come after test for space char
        pnctsN++;              //or it will count spaces, also.

      j=0;                     //reset column for new word
      ++row;                   //start a new word, on a new row 
    }                          //row is your word count 
    ++i;
  } 
  for(i=0;i<ROWS;i++)
    printf("\n %s ", words[i]);
}

make sense?

WOW ,
I didn't knew about

ispunct(s[i]);
isalpha(s[i]);

I have been searching for 2D arrays as it is very confusing to me, but your code is very beautiful.

I have "Let us C" book and was reading it all day ( i'm a slow learner (very slow))

And without the conio.h its giving me error with getch(); so i added it back.

And the out put looks nice :)

Thank you very much.

So now we get the words to the rows.

I like conio.h, but you should know that's not part of the C standard - it's an extension, and lots of C compilers don't include it.

In those cases, getchar() can replace getch(), conio.h isn't required anymore, and now you're back to standard C.

Thanks for the kind words.

Here's a little bit on arrays. For simplicity, I'm skipping over mention of the end of string char '\0', that would be present for any string.

char words[10][20] is a "true" array with 10 rows, and 20 chars in each of those rows.

Even if the word being saved is "I", that row will still have space for 20 char's in it. Very rigid.

char *words[20] is different. It's an array of 20 pointers to char. Each pointer points to a word of UP TO 20 char's. If you just save the word "I", then you only use ONE char though, instead of 20. Also, the pointers themselves can be swapped around (handy for sorting), without moving any actual words.

To actually move a word, you'd use something like strcpy(). To move a pointer to a word, you just swap the address the pointer has:

Address Word
================
0001 Apple
0002 Cherry
0003 Berry

To sort the words above, I can just change an address:
Address Word
===============
0001 Apple
0003 Cherry
0002 Berry

Which words because we can now refer to the list, by going THROUGH the pointer address, instead of the actual word. Final effect is exactly the same as if the words themselves had been sorted. Extra benefit is that the original order of the words, has not been lost - sometimes that is important to preserve.

So Words is our char pointer, for each word.

I don't want to jump you ahead of where you want to be, however. There's nothing wrong with using strcmp() and swapping the actual words, to sort them. This is just another way.

Since we'll be sorting less than 1000 words, I believe, a Selection sort would be a good choice. It's like a bubble sort, but slightly faster and easier.

Why don't you read up on Selection sort on Wikipedia, and let me know which way you want to sort these words? That would be the easiest way to find unique words, as I'll show you.

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

changed "ROWS" to "row" as its printing all 20 lines every time.

if i give blank spaces then those blank spaces are also being stored as words and getting printed:
Ex:

input string: "one two [5 spaces] three"

Output:
words[1]= one
words[2]= two
words[3]=
words[4]=
words[5]=
words[6]=
words[7]=
words[8]= three

I'm familiar with bubble sort :)

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

This doesn't seem to be a possible idea :( gives error

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

This one works better but it still stores the SPACES

I wanted to edit the above post but i cant see the EDIT button some times :(

deleted

Edits are limited by time - after so many hours, the post can't be edited.

The edit button is just above the "Post Reply" button, on the left side of the post itself, if it's possible to edit that post.

By "blank spaces" do you mean a blank line, or just a space?

I haven't looked into blank lines, yet. Spaces were not a problem, however.

for(i=0;i<wordsN;i++)  
            for(j=0;j<wordsN-1;j++)  
                 {  
                    if(strlen(words[i])>strlen(words[i+1]))  
                      {  
                       strcpy(temp,words[i]);  
                       strcpy(words[i],words[i+1]); 
                       strcpy(words[i+1],temp]);  
                      }  
                 }

Doesn't this work ??

By Spaces i mean SPACE itself not lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.