Bubble Sort Using C-Style String

This is a discussion on Bubble Sort Using C-Style String within the C++ Programming forums, part of the General Programming Boards category; Here is code that works for integer arrays. Please offer guidance on how I can make it work for C-style ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Bubble Sort Using C-Style String

    Here is code that works for integer arrays. Please offer guidance on how I can make it work for C-style strings.

    Code:
    //// strCompare.h ////
    
    
    int strCompare(char a[], char b[]);
    and

    Code:
    //// strCompare.C ////
    
    
    #include "strCompare.h"
    
    
    int strCompare(char a[], char b[])
    {   if ( a == b ) return0;    // a and b are at same location
        if (a[0]=='\0' && b[0]=='\0')  return 0;
        int ans = a[0]-b[0];
        if ( ans == 0 ) return strCompare(a+1, b+1);
        return ans;
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    You can not use the comparison operator== with C-strings. You must use strcmp().

    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,506
    Quote Originally Posted by jimblumberg View Post
    You can not use the comparison operator== with C-strings. You must use strcmp().

    Jim
    The code is checking if the address is the same therefore it is valid code.
    Note: it is possible the OP thought it was doing more than that.

    Tim S.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,506
    Quote Originally Posted by Sammy2011 View Post
    Here is code that works for integer arrays. Please offer guidance on how I can make it work for C-style strings.
    Nice code; from whom did you steel it?

    Did you try the code?
    Hint: The code is for C-style strings.

    Tim S.
    Last edited by stahta01; 11-16-2011 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    My professor gave it to us to use as a class. Ph.D from MIT. Pretty smart guy. I'm a little confused, he said it works for integer arrays but we need to modify it for c-style strings?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sammy2011 View Post
    My professor gave it to us to use as a class. Ph.D from MIT. Pretty smart guy. I'm a little confused, he said it works for integer arrays but we need to modify it for c-style strings?
    Nope, as stahta01 points out, it is already for C strings, which are stored in char arrays. You mention "integer arrays" in the first post, but I do not seen any. FYI: The reason someone would suspect you stole the code is because you obviously don't understand it, not because it is particularly clever (it isn't -- I'm guessing it is just intended to demonstrate recursion, because actually using recursion to compare strings is pretty silly).

    Perhaps you are suppose to modify it for C++ strings?
    Last edited by MK27; 11-18-2011 at 10:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    What I can't figure out is, why does the thread title have "Bubble Sort" in it.

    Your professor must have meant that it works for c-style strings but that you'd need to modify it for integer arrays.
    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"

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by iMalc View Post
    What I can't figure out is, why does the thread title have "Bubble Sort" in it.
    I was wondering that! Maybe OP intended to post different code.

    As has been said the code posted will work on C style strings. To modify it to work for integer arrays you'll need to consider that an integer array won't be NULL terminated like a C string, and figure out a way to handle that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort help
    By BNoble in forum C Programming
    Replies: 20
    Last Post: 03-31-2011, 12:16 AM
  2. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-22-2008, 11:03 PM
  3. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 08:54 PM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM
  5. Sorting a string using bubble sort?!
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 04:44 PM

Tags for this Thread


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