Thread: Sorting through an array.

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    34

    Sorting through an array.

    Hello;
    I am new to C and am having difficulty with arrays and strings.
    I have an assignment where I have to sort through the words in a sentence and determine if it contains a specific word. Honestly I don't know where to start other than with a for loop.

    Code:
    for (i = 0; i < sizeof; i++)
    {
         
    }
    I do not know how to go about sorting the string to find individual words.
    
       if(array[i] == ' '; //is this correct

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, you will need to understand a few basics: arrays, strings (closely related to arrays) and sorting algorithms. The first two should be covered in just about every tutorial, textbook and your course notes. It may or may not cover the sizeof operator; in any case you're not using it correctly in your code sample. The sorting algorithms may be a bit tougher. Wikipedia has great articles on many of the common sorting algorithms. I recommend looking up the algorithm you are to use; if you don't have one, any of bubble sort, insertion sort or selection sort are excellent choices for beginners.

    Also, since you have no idea where to start, you should not be writing any code yet. You can not program a computer to accomplish the task if you don't understand how to do the task yourself. You must understand arrays and sorting conceptually, and have a plan for how to solve this, before you write any sorting code.

    I suggest you go over any C resources you have, and work through the basics of arrays and strings. Just a few simple programs for practice would be good. A "statistics" program might be good for arrays. Entering a list of numbers into an array and calculate min, max, mean (average), standard deviation, etc would be a good, simple array exercise. It can be done without arrays, but in your case it should be done with them. Something like reading words/lines from a file or from the user/keyboard, and echoing them back, would be good starter for string handling. Maybe use strcmp to compare the strings/words entered and print out the max and min string, or to search a file for a particular word, would be good.

    Once you have the basics of arrays and strings down, start planning out your string sorting program with paper and pencil. Once you have the logic all sorted out, you can start implementing it. Work in small chunks, stopping often to compile (at the maximum warning level, of course) and fix any errors/warnings you find; test and make sure the code up to there is working perfectly too. For example, start with just being able to read all the strings you want into an array of strings. Then, print them back. Then work on sorting the array (you'll likely need strcmp and strcpy for this).

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    For what you require, you don't need any sorting algorithm. you simply need to read each word in the sentence and compare it to your target word.

    Look up the read functions (scanf, fgets, etc). There is one that will read a single word. You might even want to write a test program or two to understand the functions you need to use.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    2
    Hi,
    Well you need to understand the basics of array and strings.Then you can go further to step of sorting.The basics of array are well explained in this link. <Link Removed>

    I found it useful.Hope you will be able to understand these concepts deeply.
    Best o luck!!

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    34

    Smile

    Thank you to every one who responded. I figured out what I was doing wrong and got the code to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 04-18-2013, 02:21 PM
  3. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. Array Sorting ???
    By Takis_ in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2008, 10:38 AM