read names from a file and sort them in alphabetical order - 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.

read names from a file and sort them in alphabetical order

raeikoraeiko Posts: 17Member
Hello guys,

i need you help with a C program that reads names from a .txt file and sort them in alphabetical order.

I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them.

Could you please give me some hints?

Thanks a lot,

raeiko

Here is my code:

[code]#define SIZE 30

int main()
{
char name[ SIZE ]; /* Data records */

FILE *fpPtr; /* fpPtr = sorted_file.txt pointer */

/* fopen opens file; exits program if file cannot be opened */
if ( ( fpPtr = fopen( "sorted_file.txt", "r" ) ) == NULL ) {
printf( "File could not be opened
" );
} /* End if */

/* Read records from file */
else {
printf( "%s
", "Name" );
fscanf( fpPtr, "%s", name );

/* While not end of file */
while( !feof( fpPtr ) ) {
printf( "%s
", name );
fscanf( fpPtr, "%s", name );
} /* End while */

fclose( fpPtr ); /* fclose closes the file */

} /* End else */




return 0; /* Indicates that the program terminated successfully */

} /* End Main */
[/code]

Comments

  • ActorActor Posts: 438Member
    Your code seems to imply that there are, at most, 30 names in the file. Is there any limit to how long a name can be?

  • raeikoraeiko Posts: 17Member
    Hi,

    in my file there are 15 names and there is no limit to the names' lenght, but let's say no more than 30 characters.

    Thanks,
    raeiko
  • ActorActor Posts: 438Member
    This seems to work, but it is not something I would want to submit to a client as a completed work. There is one bug: a blank seems to act as a delimiter on input. For example, "Van Buren" is seen as two different names, whereas it should be seen as a single name.

    I'm assuming that a sort in memory is required, rather than a sort on disk. The variable [b]name[LEN][SIZE][/b] is declared as a two dimensional array with, hopefully, the first index indicating the position of a character within a string and the second index indicating which string in the matrix. Hopefully this means that [b]name[i][/b], with only one index, is a pointer to the ith string.

    C is not very good at string manipulation compared to other languages.
    [code]
    #include

    #define SIZE 30
    #define LEN 31

    int main()
    {
    char name[LEN][SIZE]; /* Data records */
    char hold[LEN] ;
    int i,j ; /* indices of array */
    int last ; /* index of last item in array */

    FILE *fpPtr; /* fpPtr = sorted_file.txt pointer */

    /*
    copy file contents to array
    */

    /* fopen opens file; exits program if file cannot be opened */
    if ( ( fpPtr = fopen( /* "sorted_file.txt" */ "namesort.txt", "r" ) ) == NULL ) {
    printf( "File could not be opened
    " );
    } /* End if */

    /* Read records from file */
    else {
    /* While not end of file */
    for(i = 0 ; !feof( fpPtr ) ; i++ ) {
    fscanf( fpPtr, "%s
    ", name[i] );
    } /* End while */
    last = i - 1 ;

    fclose( fpPtr ); /* fclose closes the file */

    /*
    sort
    */
    for (i = last ; i > 0 ; i--)
    for (j = 1 ; j <= i ; j++)
    if (strcmp(name[j],name[j - 1]) < 0) {
    strcpy(hold,name[j]) ;
    strcpy(name[j],name[j - 1]) ;
    strcpy(name[j - 1],hold) ;
    }
    /*
    write array to output
    */
    for (i = 0 ; i <= last ; i++)
    printf("- %s
    ",name[i]) ;

    } /* End else */

    return 0; /* Indicates that the program terminated successfully */
    } /* End Main */
    [/code]
  • raeikoraeiko Posts: 17Member
    Hello,

    thanks a lot for your precious help!!!

    I have a last question (it's not required in my assignment, it's just out of curiosity):

    If i want to display the names in the originla order first and then in alphabetical order,where do i place the printf statement?

    Something like:

    "The names in the original order are: blah,blah,blah"

    "The names in alphabetical order are: blah, blah,blah"


    Thanks again,

    raeiko


  • ActorActor Posts: 438Member
    [code]
    #include

    #define SIZE 30
    #define LEN 31

    int main()
    {
    char name[LEN][SIZE]; /* Data records */
    char hold[LEN] ;
    int i,j ; /* indices of array */
    int last ; /* index of last item in array */

    FILE *fpPtr; /* fpPtr = sorted_file.txt pointer */

    /*
    copy file contents to array
    */

    /* fopen opens file; exits program if file cannot be opened */
    if ( ( fpPtr = fopen( /* "sorted_file.txt" */ "namesort.txt", "r" ) ) == NULL ) {
    printf( "File could not be opened
    " );
    } /* End if */

    /* Read records from file */
    else {
    /* While not end of file */
    [red]printf("The names in original order are
    "); { outside loop }[/red]
    for(i = 0 ; !feof( fpPtr ) ; i++ ) {
    fscanf( fpPtr, "%s
    ", name[i] );
    [red]printf("%s
    ", name[i]) ; { inside loop }[/red]
    } /* End while */
    last = i - 1 ;

    fclose( fpPtr ); /* fclose closes the file */

    /*
    sort
    */
    for (i = last ; i > 0 ; i--)
    for (j = 1 ; j <= i ; j++)
    if (strcmp(name[j],name[j - 1]) < 0) {
    strcpy(hold,name[j]) ;
    strcpy(name[j],name[j - 1]) ;
    strcpy(name[j - 1],hold) ;
    }
    /*
    write array to output
    */
    [red]printf("
    The names in alphabetical order are
    ") ;{ outside loop }[/red]
    for (i = 0 ; i <= last ; i++)
    printf("- %s
    ",name[i]) ;

    } /* End else */

    return 0; /* Indicates that the program terminated successfully */
    } /* End Main */
    [/code]
  • raeikoraeiko Posts: 17Member
    OK,got it;-)

    Many many thanks!!!

    raeiko
Sign In or Register to comment.