Bubblesort with special characters

This is a discussion on Bubblesort with special characters within the C Programming forums, part of the General Programming Boards category; Based on the discussion "Problems sorting a string" I did my own bubblesort-function. It works fine except for the three ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    Bubblesort with special characters

    Based on the discussion "Problems sorting a string" I did my own bubblesort-function. It works fine except for the three Swedish characters "" (not sure if these will be visible depending on you charset, but we have three chars which are not part of the 7-bit ascii).

    These characters (and there uppercase version) are in my Linux (Ubuntu) represented by two chars each instead of one, first one char which is always the same, and then another which is of course different depending on which one it is.

    I wrote a short program to output the values of these chars but I get negative values. It looks like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char * buf="";
      int x;
      //  printf("%d",ch);
      for (x=0;x<12;x=x+2)
        printf("d 1: %d 2:%d\n",x,buf[x],buf[x+1]);
      exit(0);
    }
    I get the values, but they are negative (-61 followed by another negative number). I thought that char's can't have negative values, so how do I get the real value, or how do I use this in my code to sort these aswell?

    I don't need a complete code, just an explanation of why these are negative or how I will get the unsigned version (if they are not supposed to be negative).

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Never mind. I got it working. It doesn't look to nice, but it works now :-)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could stored them as unsigned char:
    Code:
      unsigned char * buf="&#229;&#228;&#246;&#197;&#196;&#214;";
    And print them as unsigned int with:
    Code:
        printf("&#37;d 1: %u 2:%u\n",x,(unsigned) buf[x], (unsigned) buf[x+1]);

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    The easiest way to sort a string containing both single-byte and multi-byte characters correctly would be to convert it first, perhaps to UTF16 in this case, then do the sort, then convert back if you so desire.
    It would seriously be a real pain in the ass trying to sort it without doing this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. special characters removing from srting
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 06-06-2007, 08:30 PM
  3. Special characters and sending them to programs...
    By Dragoon_42 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2006, 03:49 PM
  4. special characters
    By volk in forum C Programming
    Replies: 8
    Last Post: 02-20-2003, 02:57 AM
  5. Special (non) Keyboard Characters
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21