Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 9:15 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: String Sort
PostPosted: Thu Nov 13, 2008 6:07 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
#include <stdio.h>
#include <string.h>

/*
* Program to read a list of strings from standard input and print them in
* sorted order.  This uses a simple selection sort.
*/

#define MAX_STRING_SPACE        1000
#define MAX_NUM_STRINGS         250
#define MAX_STRING_SIZE         50
int main(void)
{
        /* Where the general string contents are stored. */
        char string_space[MAX_STRING_SPACE];

        /* The start of each string. */
        char *strings[MAX_NUM_STRINGS];

        /* Read the strings. */
        char buf[MAX_STRING_SIZE];
        char *next_space = string_space;
        int inloc = 0;
        while(scanf("%s", buf) == 1) {
                /* Find the length of the string and see if it fits. */
                int length = strlen(buf) + 1;
                if(next_space + length >= string_space + MAX_STRING_SPACE)
                        break;
                if(inloc >= MAX_NUM_STRINGS)
                        break;

                /* Place the string into the structure. */
                strings[inloc++] = next_space;
                strcpy(next_space, buf);
                next_space += length;
        }

        printf("--------------------------------------------------\n");

        /* Perform the sort.  Outer loop goes through destination of the
           minimum string. */
        int strloc;
        for(strloc = 0; strloc < inloc - 1; ++strloc) {
                /* Scan the remaining strings for ones smaller. */
                int scan;
                for(scan = strloc + 1; scan < inloc; ++scan) {
                        if(strcmp(strings[strloc], strings[scan]) > 0) {
                                /* Exchange the strings. */
                                char *tmp = strings[strloc];
                                strings[strloc] = strings[scan];
                                strings[scan] = tmp;
                        }
                }
        }

        /* Print 'em. */
        for(strloc = 0; strloc < inloc; ++strloc) {
                printf("%s\n", strings[strloc]);
        }
}


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 replacing each tab by the string “Tab”,
 Library Sort
 Breaking the String into Words
 search for string in a cell array
 Trim string in JavaScript
 How to read string text in files
 add string to another, Lowercase ,Uppercase
 search in a string and replace
 get number of words in a string
 sort words in c++

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team