Thread: Complete beginner needs help customising sorting code

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Complete beginner needs help customising sorting code

    Hi all.
    I've got this code here
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
        //jumbled up text in array
        char strarray[] = "fgjhsflsdlkfghdksdkjdgskakdkfkjggkdkgjg";
        int i = 0;
        int j = 0;
        char temp;
        void Bubble(char* strarray, int arrsize);
        
    int main()
    
    
        {
        cout << strarray << endl;
        Bubble(strarray, strlen(strarray));
        cout << strarray << endl;
            
        return 0;
    }
    
        
    void Bubble(char* strarray, int arrsize) //Bubble Sort code
    
    
        {
            for(i=0; i< (arrsize - 1); ++i)
    
    
                {
                    for(j = i + 1; j > 0; --j)
    
    
                        {
                            if(strarray[j] < strarray[j-1])
    
    
                                {
                                    //Swaps the values
                                    temp = strarray[j];
                                    strarray[j] = strarray[j - 1];
                                    strarray[j - 1] = temp;
                                }
                                
                            }
                        }
                }
    which aparantly can sort text. I need to customize this code to do the following - search through some text, sort all of the numbers, and arange them in numerical order, break up all the letters, and sort them alphabetically ( capitals first), then arange all the punctuation in a specific order, so if it takes a text like this:
    I remember 4/12/1987.
    it will come out something like this:
    4
    12
    1987
    b
    e e e
    I
    m
    m
    r
    r
    .
    / /
    Is that possible with this code? Could someone please point me in the right direction - bearing in mind I am a complete begginer
    thanks a lot
    Last edited by Salem; 11-04-2007 at 01:47 AM. Reason: Replace php tags with code tags

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Yes it is possible with bubble sorting (selection sorting is way faster, but the program is so small it doesnt matter). You need to deside if the current index it is on (the current number/letter in line) is a double/int, or a character. Once that is desided store it into a temporary array (one for integers/doubles, and one for characters). Then sort each temporary array according to what needs to be done, in your case integers are lowest to highest, then characters capitol to lowercase. Then read the temp arrays back to the original array in the correct order.

    If you followed that, awesome, if not.. i don't blame you. I thought of that as i was typing.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    wow, thanks. how do I create an array then? lol

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    char strarray[] = "fgjhsflsdlkfghdksdkjdgskakdkfkjggkdkgjg";
    http://www.cprogramming.com/tutorial/lesson8.html

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    fantastic, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Sorting
    By jazzz in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 07:48 AM
  4. A beginner needs help...
    By Flacon in forum C Programming
    Replies: 1
    Last Post: 11-23-2005, 02:56 AM
  5. Complete list of vB code colors?
    By SilentStrike in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-30-2001, 06:58 PM