sorting character array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sorting character array


hi
I have problem in sorting the array alphbetically.
I have the names of students only first names.I have the function which works but doesn't gives the consistent result as it should .
So can any one help in getting the problem to be solved
my array declaration is :

char *names[100];

Best REgards
Jhon

Comments

  • prasunprasun Posts: 46Member
    :
    : hi
    : I have problem in sorting the array alphbetically.
    : I have the names of students only first names.I have the function which works but doesn't gives the consistent result as it should .
    : So can any one help in getting the problem to be solved
    : my array declaration is :
    :
    : char *names[100];
    :
    : Best REgards
    : Jhon
    :

    have u tried strcmp or stricmp functions

  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    : : hi
    : : I have problem in sorting the array alphbetically.
    : : I have the names of students only first names.I have the function which works but doesn't gives the consistent result as it should .
    : : So can any one help in getting the problem to be solved
    : : my array declaration is :
    : :
    : : char *names[100];
    : :
    : : Best REgards
    : : Jhon
    : :
    :
    : have u tried strcmp or stricmp functions
    :
    :

    If you are still having problems, post your code so that someone can help you.

  • sweettamsweettam Posts: 6Member
    : : :
    : : : hi
    : : : I have problem in sorting the array alphbetically.
    : : : I have the names of students only first names.I have the function which works but doesn't gives the consistent result as it should .
    : : : So can any one help in getting the problem to be solved
    : : : my array declaration is :
    : : :
    : : : char *names[100];
    : : :
    : : : Best REgards
    : : : Jhon
    _______________________________________________________________________
    // I hope this will rectify ur problem
    #include
    void main()
    {
    int i,n;
    char *names[100]; // your actual Array Declaration
    char temp[20]; // temperory Char Array to store the Readed Sring
    printf("Enter total no Of Students");
    scanf ("%d",&n);
    for(i=0;i0)
    {
    strcpy(temp,name[i]);
    strcpy(name[i],name[j]);
    strcpy(name[j],temp);
    }
    // Print the Sorted List of Student names
    for(i=0;i<n;i++)
    printf("
    %s",name[i]);
    }


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]It is not bad, but few fixes needed:[/blue]
    [code]
    : // I hope this will rectify ur problem
    : #include
    : [red][b]int[/b][/red] main()
    : {
    : int i,n[red][b],j[/b][/red];
    : [red][b]int bSwapped=1;[/b][/red]
    : char *names[100]; // your actual Array Declaration
    : [red][b]char* pstr;[/b][/red]
    : char temp[20]; // temperory Char Array to store the Readed Sring
    : printf("Enter total no Of Students");
    : scanf ("%d",&n);
    : for(i=0;i0)
    : {
    : [red][b]pstr=name[i];
    : name[i]=name[j];
    : name[j]=pstr;
    : bSwapped=1;[/b][/red]
    : }
    : [red][b]}[/b][/red]
    : // Print the Sorted List of Student names
    : for(i=0;i<n;i++)
    : printf("
    %s",name[i]);
    : [red][b]return 0;[/b][/red]
    : }
    : [/code]
    :
    :

Sign In or Register to comment.