Thread: names in alphabetical order?

  1. #1
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62

    Question names in alphabetical order?

    plz help me write a programme that asks user to input different names and then sort them i alplabetical order

  2. #2
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Have you done anything? Do you have any idea and you are stuck? You ve got to give us something to work with.
    Obviously you must use a sort algorithm. Do you have something specific in mind?

    How have you declared your array. Have you written something like char name[][] or have you written something like char *names[] where each pointer will show to a name?

    And also

    http://www.google.com/search?hl=en&q...+c&btnG=Search
    Last edited by Phoenix_Rebirth; 02-05-2009 at 08:14 AM.

  3. #3
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    i know how to sort numbers in ascending/descending order... but i am confused with strings ! i dunno how to use strings in array ?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    names[row][col] is normal, where col = # of columns

    Post up your trial code, and we'll assist. We won't assist otherwise. That's a necessary policy because we just become a homework service for lazy students, otherwise.
    Last edited by Adak; 02-05-2009 at 08:16 AM.

  5. #5
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    char s[100];
    gets(s);
    .
    .
    .
    that is how i input string. so now s[0] is the first character in string, s[1] is second character and so on... but that is all about single string only... how can i define strings in array... a simple example could help me!

  6. #6
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    i am new to c programming, plz forgive if i sound like stupid...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So why would it be different from anything else? An array of something is declared as (something)[]. Since in this case, the something is char [], your array of strings will be char[][].

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No sweat necrolord. How about an include file for standard io? We'll need that.

    Code:
    /*
    makes an array for 5 names (names[0] - names[4], which can each have up to 24 letters in their name.
    The last space we'll save for the end of string char: '\0', which C uses to mark the end of any string.
    */
    char names[5][25];

  9. #9
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    oh thanks adak.. so that is how i can define strings in an array... that was all i needed for now..

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    qsort!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int cmp(void *a, void *b);
    
    int main()
    {
        char *names[5];
        char name[80];
        int i, j;
        printf("enter up to 5 names> ");
        for (i = 0; i < 5 && gets(name); i++) names[i] = strdup(name);
        qsort(names, i, sizeof(char*), cmp);
        for (j = 0; j < i; j++) puts(names[j]);
        return EXIT_SUCCESS;
    }
    
    int cmp(void *a, void *b)
    {
        return strcmp(*(char**)a, *(char**)b);
    }

  11. #11
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    thx meldreth but i would like to it on my own with some help by you guys .. btw, what does qsort do?


    Code:
    int i;
    typedef char* string;
    char name[10][50];
    for(i=0;i<10;i++)
    {
        printf("Enter a name : ");
        gets(name);
    }
    I am still unable to input the string...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    name is an array of strings. You should be reading into name[i]. Oh, and instead of using gets(), use fgets() so that you can specify the maximum string length.
    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

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by n3cr0_l0rd View Post
    btw, what does qsort do?
    qsort puts things in order based on the last argument. since the last argument in my example was strcmp, it puts the strings in alphabetical order.
    Quote Originally Posted by n3cr0_l0rd View Post
    Code:
    int i;
    typedef char* string;
    char name[10][50];
    for(i=0;i<10;i++)
    {
        printf("Enter a name : ");
        gets(name);
    }
    I am still unable to input the string...
    name is a 2d array and gets expects a 1d array. change it to gets(name[i]).

  14. #14
    Registered User n3cr0_l0rd's Avatar
    Join Date
    Feb 2009
    Posts
    62
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main()
    {
      int i;
      typedef char* string;
      char name[10][50];
      for(i=0;i<10;i++)
      {
        printf("Enter a name : ");
        fgets(name[i]);
      }
      printf("%s",name[2]); /* just checking if my input is working */
      getch();
    }
    Error: noname00.cpp(11,4):Illegal character '' (0x1)
    Error: noname00.cpp(13,19):Too few parameters in call to 'fgets(char *,int,FILE *)'

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by n3cr0_l0rd
    Error: noname00.cpp(11,4):Illegal character '' (0x1)
    there's a control character right after the second opening curly brace. just clear that line and type it again.
    Quote Originally Posted by n3cr0_l0rd
    Error: noname00.cpp(13,19):Too few parameters in call to 'fgets(char *,int,FILE *)'
    fgets expect three arguments: an array to populate, the size of the array, and an input stream. try fgets(name[i], 50, stdin) instead. stdin is the stream that gets uses automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-14-2008, 03:52 PM
  2. Putting a word in alphabetical order.
    By Brewer in forum C Programming
    Replies: 12
    Last Post: 12-16-2006, 05:11 PM
  3. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  4. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM
  5. Help me sort the names in alpha order.
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 02:30 PM