sorting arrays of characters or strings in C

sorting arrays of characters or strings in C

Page 1 of 1

2 Replies - 1313 Views - Last Post: 22 December 2008 - 09:13 AM Rate Topic: -----

#1 zms21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-December 08

sorting arrays of characters or strings in C

Posted 21 December 2008 - 08:17 PM

Can anyone help me on how sort strings or characters in an array using c....please...i badly needed your help.....i just dont have any idea on how to do it...how can i sort the names of the students in this program....??????
_______________________________________________________
#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib>
#include<string.h>
//#include<listwind.h>
#define SIZE 5

main()
{

struct student
{
char fname[100],lname[100];
int id,gpa;
//vector<char> fname[100];

}
studinfo[SIZE];

int n,h=0,i=0,f=0;
char choice;



printf("\t___________________________________________________\n");
printf("\tIDNumber\tFirst Name\tLast Name\tGPA\n");
printf("\t___________________________________________________\n\t");
for (n = 0; n < SIZE; n++)
{
	scanf("\t%d\t%s\t%s\t%d",&studinfo[n].id, studinfo[n].fname,studinfo[n].lname,&studinfo[n].gpa);
   printf("\t");
   }

printf("\n\n\tSTUDENTS LISTED:\n\n");
printf("\t___________________________________________________\n");
printf("\tIDNumber\tFirst Name\tLast Name\tGPA\n");
printf("\t___________________________________________________\n\t");

for (n = 0; n < SIZE; n++)
  {
  printf("%d\t\t %s\t\t %s\t\t %d\n",studinfo[n].id, studinfo[n].fname,studinfo[n].lname,studinfo[n].gpa);
  printf("\t");
  }

do{
	cout<<"\n\n\n";
	cout<<"\t[X] Exit\n";
	cout<<"\t[A] Sort Ascending\n";
	cout<<"\t[D] Sort Descending\n";
	cout<<"\t[H] Highest Gpa\n";
   cout<<"\tCHOICE: ";
	cin>>choice;





Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: sorting arrays of characters or strings in C

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: sorting arrays of characters or strings in C

Posted 21 December 2008 - 08:24 PM

Please edit your posting to follow Posting Rule 3.

Please post code like this:
:code:

Thank you for helping us helping you.

Reading your code unformatted is more trouble than I am prepared to put myself to.
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: sorting arrays of characters or strings in C

Posted 22 December 2008 - 09:13 AM

Try adding some simple bubble sorts to your program like this ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 3

struct student
{
    char fname[32],
         lname[32];
    int  id,
         gpa;
}; /* <-- Note the semi-colon used here. */

typedef struct student stud;

/* using a global array to keep it simple ... */
stud studinfo[SIZE];

void show();
void sorta();
void sortd();

/* a compare function ... */
int cmpstud( stud s1, stud s2 )
{
    int fn = strcmp(s1.lname, s2.lname);
    if(fn == 0) 
        return strcmp(s1.fname, s2.fname);
    return fn; 
}


int main()
{
    int n;
    char c, choice;
    
    printf("Enter <IDNumber space firstName space lastName space GPA> & press 'Enter'\n");
    printf("\t___________________________________________________\n");
    printf("\tIDNumber\tFirst Name\tLast Name\tGPA\n");
    printf("\t___________________________________________________\n\t");
    for(n = 0; n < SIZE; ++n)
    {
        scanf
        (
            " %d %s %s %d",
            &studinfo[n].id, 
            studinfo[n].fname,
            studinfo[n].lname,
            &studinfo[n].gpa
        );
        while((c=getchar())!='\n') c=getchar(); // flush stdin ...
        printf("\t");
    }

    show();

    do
    {
        printf("\n\n\n");
        printf("\t[X] Exit\n");
        printf("\t[A] Sort Ascending\n");
        printf("\t[D] Sort Descending\n");
        printf("\t[H] Highest GPA\n");
        printf("\tCHOICE: ");
        c = getchar();
        choice = c;
        while(c!='\n') c=getchar(); // flush stdin ...
        
        if(choice=='a' || choice=='A') 
        {
            sorta();
            show();
        }
        else if(choice=='d' || choice=='D')
        {
            sortd();
            show();
        }
        else if(choice=='h' || choice=='H') 
            printf("\n\tNot implemented yet.\n");
        else
            printf("\n\tNot a  valid choice ...\n");
            
    }while( !(choice=='x' || choice=='X') );
    
    return 0;
}

void sorta()
{
     int i, j, swap;
     stud temp;
     i = 0;
     do
     {
          swap = 0;
          for( j = 1; j < SIZE-i; ++j )
          {
            if( cmpstud(studinfo[j-1], studinfo[j]) > 0 )
            {
                temp = studinfo[j];
                studinfo[j] = studinfo[j-1];
                studinfo[j-1] = temp;
                swap = 1;                  
            }          
          }
          ++i;
     }
     while(swap);
}

void sortd()
{
     int i, j, swap;
     stud temp;
     i = 0;
     do
     {
          swap = 0;
          for( j = 1; j < SIZE-i; ++j )
          {
            if( cmpstud(studinfo[j-1], studinfo[j]) < 0 ) /* swap addresses */
            {
                temp = studinfo[j];
                studinfo[j] = studinfo[j-1];
                studinfo[j-1] = temp;
                swap = 1;                  
            }          
          }
          ++i;
     }
     while(swap);
}

void show()
{   
    printf("\n\n\tSTUDENTS LISTED:\n\n");
    printf("\t___________________________________________________\n");
    printf("\tIDNumber\tFirst Name\tLast Name\tGPA\n");
    printf("\t___________________________________________________\n\t");
    int n;
    for(n = 0; n < SIZE; ++n)
    {
        printf
        (
            "%8d %17s %14s %9d\n",
            studinfo[n].id, 
            studinfo[n].fname,
            studinfo[n].lname,
            studinfo[n].gpa
        );
        printf("\t");
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1