Alphabetizing

WeeZer51402

Registered
Right no im working on a server that reads a database from a file into a structure, this database includes last name, first name/email/phone number, the way im going to set it up so people can add entries is by making it a linked list but then the question becomes how do i alphabetize the list by last name and how could i tell it to just search for 'a's
thanks,
mike
 
well i tried writing a bubble sort program to do it but it doesnt quite work right...

Code:
#include <stdio.h>

int main (void) 
{
    char string[6];
    int x,y,temp=0;
    
    printf("Enter 5 Letters: ");
    scanf("%s", &string);
    for(x=0; x<6; x++)
        for(y=0; y<6; y++)
            if(string[y] > string[y+1])
            {
        
            temp=string[y+1];
            string[y+1]=string[y];
            string[y]=temp;
            }
    for(x=0; x<7; x++)   
        printf("%c", string[x]);
    printf("\n");
        
    return 0;
}

Update: i fixed the program, but i would like to make that array string, dynamic so the user can input more info than just 5 chars, malloc maybe?
 
ok, i made it so you arent so limited in your input, the problem i was getting at that printf function was with the string length of 'string', there were termination characters in it, heres what does work though, get the strlength of a pointer, storing it in a variable and then copying its contents into 'string'.


Code:
#include <stdio.h>

char *str[];

int main (void) 
{
    char string[1000];
    int x,y,temp=0,i=0,a;
    printf("Enter Less Then 1001 Letters: ");
    scanf("%s", &str);
    a=strlen(str);
   
    strcpy(string, str);
    
    for(x=0; x<(a+1); x++)
        for(y=0; y<(a+1); y++)
            if(string[y] > string[y+1])
            {
                temp=string[y+1];
                string[y+1]=string[y];
                string[y]=temp;
            }
            
    for(i=0; i<(a+2); i++)
            printf("%c", string[i]);
                
    printf("\n");
        
    return 0;
}
 
any help...anybody, i need to make this dynamic, i dont want to declare 'char string[1000]', also now that i can alphabetize how do i take a file and alphabetize strings from that file? ex.

mike
bob
george
fred
charles


how could i make a program read that from a file and sort it?
 
Back
Top