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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.