Thread: Reordering strings!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Reordering strings!

    Code:
    #include<stdio.h>
    int main()
    {
        int i,j,n;
        char a[50],t[50];
        printf("Enter number of terms");
        scanf("%d",&n);
        printf("enter strings");
        for(i=0;i<n;i++)
        {
            scanf("%s",&a[i]);
         }
        for(i=0;i<n;i++)
        {
           for(j=0;j<n-i-1;j++)
           {
               if(strcmp(a[j],a[j+1])>0)
               {
                   strcpy(t,a[j]);
                   strcpy(a[j],a[j+1]);
                   strcpy(a[j+1],t);
               }
            }
         }
        for(i=0;i<n;i++)
        {
           printf("%s",a[i]);
         }
        return(0);
    }
    I am getting "segmentation fault" every time i execute it.Please help
    Last edited by Monochrome; 11-02-2011 at 07:41 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    The problem is that a is an array of char, but you want a[i] to be an array of char, not a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lose the & on line 11

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Incidentally, you probably should enable compiler warnings because the central mistake in your declaration of a should have shown up as warnings in your use of strcmp, strcpy and printf with %s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    This is more complex in C then in other high level languages. Important thing to know is that C doesn't have a string type - strings are implemented as null-terminated arrays of characters, these are widely called C-strings. Caveat is that a character array is not a string unless it is null-terminated.

    On to the concrete.
    Code:
    char str[3];
    str[0] = 'a';
    str[1] = 'b';
    str[2] = 'c';
    // `str` is not a string, but an array of char, containing 'a', 'b' and 'c'.
    
    str[2] = '\0';
    // `str` now a C-string, as it is null-terminated, and can be used
    // with C library's string manipulation functions.
    To get an array of strings, you can declare an array of char arrays, just like so:
    Code:
    char str_array[5][20];
    // `str_array` is an array of 5 20-element char arrays; you can use it
    // to store 5 C-strings, each 19 chracters long (19 characters + null-byte).
    
    // Used like this...
    for (int i = 0; i < 5; ++i) {
            scanf("%19s", str_array[i]);
            // `"%19s"` for read at most 19 characters; null-byte is added as 20th by `scanf()`;
    }
    Alternatively, you could declare `str_array` as an array of pointers-to-char and use it to store pointers to dynamically allocated memory that would actually hold your strings. This would free your program of arbitrary limitations on string length. Implementation is left as an exercise.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Reordering numbers
    By shaheel in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 08:56 AM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM