Thread: array search

  1. #1
    Registered User
    Join Date
    Oct 2006

    array search

    i've been working in c for a few months now, but am relatively new to the concept of arrays. the program that i am working on requires me to read in a string of up to 20 unique characters into an array, and then call a function to read the string, identify the unique characters, and print the characters out on a single line with no white spaces.

    Input: Hello world!
    Output: Helowrd!

    that being said my question is how to read through the array (without using a string) and determine which characters are unique, and which have already been used.

    i hope this makes sense, and i appreciate any help i can get.

    below is how i would write the code if i had a string library available...
    its just writing this without strings that is throwing me.

    #include <stdio.h>
    #include <string.h>
    int main(int argc, char** argp)
        char Text[] = "Hello World";
        size_t ix;
        for(ix=0; ix<strlen(Text); ix++)
            char c = Text[ix];

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    The easy way to do this is simply to ignore any duplicate input.
    repeat up to 20 times
        read one character
        see if it's in the array
        if it isn't, put it in at the first free spot
        if it is, leave it out
    Just use an array of 20 characters, since you have stated that's all you'll be reading. Set it all to zero to start, keep a counter of how many unique items are already stored, so you'll know where to stop at when you loop through checking for dupes.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    okay so i've got my array set to 20 characters, and i initialize it at 0, what is the best method for deciding if a character is already stored or not? and if it is unique just use a putchar() to send it to the array?

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Run a loop from 0 to < however many things you have already stored in your array so far. For each itteration of the loop compare the array element at that position with the character you are trying to tell the uniqueness of. If it is unique (i.e. you get to the end of the loop and haven't found a match), store it in the first "free" element of the arary and increase your counter of however many things you have already stored in your arary sa far. putchar() doesn't come into it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 07:33 AM
  5. binary search in an array
    By brianptodd in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2002, 02:05 PM