Thread: help please..alphabetic sorting.

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Question help please..alphabetic sorting.

    Hello. I've written a program that sorts the words it took from a file alphabetically. And it works fine with standart characters. But I am from turkey and I need it to recognise turkish characters either. And that, I don't know how. If you know how to do this, please help me. Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcmp with your local set correctly for your compiler should handle it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or locale even
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    yeah but can you explain me how to use this locale thing. I include the locale.h first. Then what must i do?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    Or locale even
    I only need to know how to spell function names, and to make sure all my variables, however misspelled, are the same misspelling.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    quzah, i still don't know how to use locale. please explain

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    clicky click

    You could of course go to your favourite search engine and put in some magic words, like 'rapums rapums' ... er ... 'C programming set locale', instead of waiting around for me to reply...


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    I did of course. I've learned that i need to call function setlocale(LC_ALL,""). And it makes the program recognize turkish letters which it already did by some other way.Now it puts the turkish letters at the end of the array without any order, after it sorts the standard letters. Thats why I've been asking again, am I doing something wrong? thanks.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It sorts them numerically. Each letter has a value assigned to it. Do something like this, and you'll see how it sorts each letter:
    Code:
    #include<stdio.h>
    #include<ctype.h>
    int main( void )
    {
        int x;
        for( x = 0; x < 256; x++ )
        {
            if( isprint( x ) )
                printf("%c%c", x, x % 32 ? ' ' : '\n' );
        }
        printf( "\n" );
        return 0;
    }
    strcmp is going to start with whatever letter is listed first, and considers that before anything after that in the list.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could try posting some code.
    By the way, I think you need to use strcoll() not strcmp() if you want locale-aware sorting order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    hi again, thanks for the help. strcoll worked perfect and problem is solved, but i've got a new one. now 002.txt is the list of words to be sorted. There are 4600 lines in the list and one word each. Now I first want it to count the number of words in the list by counting the '\n' and set size as this.The print the size for me to see.
    As I mentioned, it should count 4600, but it counts 488965236 or something like that which means to me that it skips the end of file and I don't know why. This is the first problem. Please help if you can and I'll be thankfull and also asking some more probably Thanks a lot.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<locale.h>
    
    
    int main ()
    {
        setlocale(LC_ALL, "");
        FILE *pt; 
        FILE *pt2;
        long size;
        pt=fopen("C:\\002.txt","r");  
        pt2=fopen("C:\\003.txt","w");
        do{
           if(getc(pt)=='\n')
           size++;
           }while(!feof(pt));
        rewind(pt);
        printf("%d",size);  
        char a[size][50];
        int i,j;
        for(i=0;i<size;i++) fscanf(pt,"%s",&a[i][0]);  
        char temp[50];
        for(i=0;i<(size-1);i++)
        for(j=0;j<(size-1);j++)
        if(strcoll(&a[j][0], &a[j+1][0]) >0 )
                            {
                            strcpy(temp,&a[j][0]);
                            strcpy(&a[j][0], &a[j+1][0]);
                            strcpy(&a[j+1][0], temp);
                            }
        for(i=0;i<size;i++)
        fprintf(pt2,"%s\n",&a[i][0]);
        getch();
        return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > it should count 4600, but it counts 488965236 or something like that
    Try initialising size to 0 before you start counting.

    There is much in your code which isn't standard C
    > char a[size][50];
    C neither has variable sized arrays, nor mixed declarations and statements.

    This would be better for TWO reasons
    Code:
    char (*a)[50];
    /* now work out the size */
    
    /* now allocate the space */
    a = malloc ( size * sizeof *a );
    1. It's valid C
    2. It means you can do this in your exchange, which is FAR more efficient than copying the strings.
    Code:
    char *temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
    Also look up the qsort() function, which will be more efficient than the bubble sort you have implemented.

    > fprintf(pt2,"%s\n",&a[i][0]);
    You can drop the & and [0], and just have
    fprintf(pt2,"%s\n",a [ i ] );
    Ditto in all the other places where you have the same style.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    Thank you for your time again. I'll look up qsort().Now that is how my code looks like after I did as you said.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<locale.h>
    
    int main ()
    {
        setlocale(LC_ALL, "");
        FILE *pt; 
        FILE *pt2;
        long size=100;
        pt=fopen("C:\\002.txt","r");  
        pt2=fopen("C:\\003.txt","w");
        char (*a)[50];
         do{
           if(getc(pt)=='\n')
           size++;
           }while(!feof(pt)); 
        rewind(pt);
        printf("%d",size);
        a = malloc ( size* sizeof *a ); 
        int i,j;
        for(i=0;i<size;i++) fscanf(pt,"%s",a[i]);  
        char *temp;
        for(i=0;i<(size-1);i++)
        for(j=0;j<(size-1);j++)
        if(strcoll(a[j], a[j+1]) >0 )
                            {
                            char *temp = a[j];
                            *a[j] = a[j+1];
                            *a[j+1] = temp;
                            }
        for(i=0;i<size;i++)
        fprintf(pt2,"%s\n",a[i]);
        getch();
        return 0;
    }
    in the
    Code:
    a[j] = a[j+1];
    a[j+1] = temp;
    lines I had a "incompatible types in assignment" error so I add the "*"'s.There are two problems.
    First, now it doesnt sort. It just changes the first letter of each word with a weird character. And second, my first problem continues. The size... It still counts more than there is.Do you see anything wrong with this part:
    Code:
    do{
           if(getc(pt)=='\n')
           size++;
           }while(!feof(pt));

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you setting it to 100? Where are you pulling that number out of? Your ass apparently.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    100 was miswritten, definition is "size=0". sorry for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM