Thread: C Function to sort a string based on a delimiter

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    C Function to sort a string based on a delimiter

    I Need to write a function using C wherein I should do the followingi) The function will receive a string in a character pointer(ii) This string will adhere to the following structure: "Kentucky+New York+Arizona+Nevada" The number of states can differ from 4 to 50 The delimiter between States can differ from '+' to ',', hence I would like to pass the delimiter to the function.(iii) This string should then be sorted alphabetically from left to right. The above example would then become: "Arizona+Kentucky+Nevada+New York"(iv) This string needs to be returned from the function using a character pointer.Please help me in this.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    For starters, try to come up with an algorithm to solve the problem.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    see: strtok(), strcmp().
    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

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    23
    Quote Originally Posted by mehimadri View Post
    I Need to write a function using C wherein I should do the followingi) The function will receive a string in a character pointer(ii) This string will adhere to the following structure: "Kentucky+New York+Arizona+Nevada" The number of states can differ from 4 to 50 The delimiter between States can differ from '+' to ',', hence I would like to pass the delimiter to the function.(iii) This string should then be sorted alphabetically from left to right. The above example would then become: "Arizona+Kentucky+Nevada+New York"(iv) This string needs to be returned from the function using a character pointer.Please help me in this.
    You should probably use strtok in order to divide your string into substrings (see here a tutorial on how to use strtok properly).
    Then, you will probably need to put them into a string array and use a sorting algorithm to arrange them in the correct order. After that you will need to iterate through your string array and concatenate them into another string. (see here some examples on how to copy, concatenate and compare strings).

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    I personally don't like strtok because it destroys the original string. You could copy it to another to keep the text intact, but that seems wasteful.

    There are a number of string functions that will find a delimiter and/or set of characters and return an index or pointer to it.

    strpbrk
    strspn
    strchr

    These will return a pointer or index to the first found, but by creative use of pointers, you can feed subsequent calls with a char pointer to where you want the search to start.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cynic View Post
    I personally don't like strtok because it destroys the original string.
    But that's a good thing! Unless you need to keep the original string intact, of course.

    You could copy it to another and keep the text intact, but that seems wasteful.
    Any method of extracting substrings which doesn't destroy the original must do that at some point.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to parse a string with no delimiter?
    By manasij7479 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2011, 05:37 PM
  2. Replies: 12
    Last Post: 04-20-2009, 06:44 AM
  3. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  4. How to set a delimiter for each line's string?
    By Mathsniper in forum C Programming
    Replies: 1
    Last Post: 12-14-2006, 10:09 AM
  5. Replies: 2
    Last Post: 10-03-2002, 03:43 PM