Thread: Word Sort Program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Word Sort Program

    I need a program to sort the words of a string leixographically in C. For example : INPUT- how are you

    OUTPUT: are how you

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post your attempt.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Code:
    #include <stdio.h>
    
    int main()
    {
      char arr[100];
    int count =0;
    int j,i,k;
    char temp;
    printf("enter the string\n");
    gets(arr);
    for(i=0;arr[i]!='\0';i++)
    count++;
    printf("value of count is %d\n",count);
    for(k=0;k<=count;k++)
    {
    for(j=0;j<count-1;j++)
    {
       if(arr[j]>arr[j+1])
       {
         temp=arr[j];
         arr[j]=arr[j+1];
         arr[j+1]=temp;
       }
    
    }
    }
    puts(arr);
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    But its a futile effort.Its not working!!!

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    How does your compiler not warn you about using gets() > FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    You'll want to replace the first loop (that simply gets the length) with something that actually counts words.

    From there, many potential approaches are possible. For example, once you know the number of words, you can then split the string up into a temporary array of "words", and then copy them back into the array in order.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <string.h>  //for strcmp(), strcpy()
    
    int main(void)
    {
      //char arr[100];
      char words[100][50];{
      {"It"},
      {"would"},
      {"be"},
      {"as"},
      {"easy"},
      {"as"},
      {"breathing"}
    };
    int count =0;
    int j,i,k;
    //char temp;
    char temp[50]; //enough to hold one word
    printf("enter the string\n");
    //gets(arr);
    //never use gets! Totally unsafe
    //an example: fgets(arr[i], sizeof(arr[i], stdin);
    //remember that fgets WILL add the \n onto the end of a string, if possible, so remove it before making any
    //string comparisons with:
    /*
    int len = strlen(arr[i]);
    if(arr[i][len]=='\n')
       arr[i][len]='\0';  //overwrite the newline and shorten the string 
    
    Yes, it's a bit of a pain, but if you continue to use gets(), you're just writing code that any hacker can exploit with well
    known attacks, to control the system, eventually.
    
    */
    
    count = 7;
    for(j=0;j<count-1;j++)
    {
       if((strcmp(arr[j], arr[j+1])) < 0)
       {
           strcpy(temp, arr[j]);
           strcpy(arr[j], arr[j+1]);
           strcpy(arr[j+1], temp);
    
       }
    
    }
    for(i=0;i<count;i++)
      puts(arr[i]);
    
    return 0;
    }
    I did not test the above, but give it a shot. In C, you can't compare strings directly. You have to use strcmp() (or write your own code for that purpose).
    Last edited by Adak; 03-19-2012 at 12:49 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Thank you...but I don't understand one thing , why using gets() makes my program vulnerable to hackers?

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because gets does not provide a mechanism to specify the number of characters you want to read, therefore potentially overflowing your storage container and rewriting areas of memory that would jump execution to a sector of malware code maliciously inserted by a hacker, instead of your own code. This is what is called a buffer overflow attack and I am sure you can come across more literature on it on Google.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    This is probably the best link out there explaining how the attack works..

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    well i think you would need to strtok each word using the strtok function in the header and then search for the beginning of each string ordering the items as you want and then print.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Word count program
    By Jpeg6 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 10:34 PM
  3. Word Sort
    By abh!shek in forum C Programming
    Replies: 32
    Last Post: 02-04-2008, 12:52 PM
  4. Word Sort--Again, but this time in C++
    By abh!shek in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2008, 12:19 PM
  5. Word Counting Program Help
    By rdave1 in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2005, 04:30 PM