Like Tree2Likes
  • 1 Post By esbo
  • 1 Post By Click_here

Auto-Complete Program in c

This is a discussion on Auto-Complete Program in c within the C Programming forums, part of the General Programming Boards category; Hey everyone, I'm new to this site and to C programming as well. I started school this year in Computer ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Auto-Complete Program in c

    Hey everyone,

    I'm new to this site and to C programming as well.
    I started school this year in Computer Science and today i'm stuck with an assignment.

    I hope i'm clear with the following question. I just need help on what t do.

    In my assignment I need to create an Auto-Complete program.
    They're asking me to create a program where the user will put a series of words of his choice. ex: hello, hey, harry, world, wait, where, etc and the program will basically autocomplete the word when i start typing it's letter.

    Basically i need to use Dynamic Memory, Structs, Data Base, Linked lists and generic functions. This is a summary of what i have.

    I hope i'm clear with the question i only need to know where and with what to start. From there i think i can continue on my own.. i hope.

    Thanks and i think it's a sick website!

  2. #2
    Registered User Dabz's Avatar
    Join Date
    Jan 2013
    Location
    Durham, UK
    Posts
    3
    Basically i need to use Dynamic Memory, Structs, Data Base, Linked lists and generic functions.


    Wow, all that just for a simple auto complete feature!?!?!

    Here's how I would do it, which, I wont use all the above, but, basically:-

    1) Create a string vector that includes all your keywords
    2) Track what the user is typing, basically, from the carat position to the first none-alpha (or none-alphanumeric) character at the left of the carat... So, if we had "hello the|", (Where | represents the carat), we'd pick out the "the" token.
    3) Loop through the vector of your keywords looking for matches of the token, basically, you want to check (e.g.) if(Left(keywords.at(n),token.size()) == token)
    4) If the token matches the beginning of any keywords, store them in a vector
    5) Use the vector to display the list of any keywords which match, or populate a list box or something where you can navigate using the arrow keys or click with a mouse.
    6) When the required keyword is highlighted and the user either completes the keyword, or hits a command key such as return or tab, insert the selected listbox token into where your typing, which will probably mean removing the beginning of the token, THEN, inserting the whole keyword, or, inserting the rest of the keyword, it's up to you.

    There are a few ways to optimize this, instead of checking every keyword, you could structure the keywords in order of length, then, only check keywords that are bigger or equals to token.size()!

    Hope that helps

    Dabz
    Last edited by Dabz; 01-13-2013 at 01:50 PM.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Can anyone tell me what a string vector is?
    My under standing is that vector is a number with size and direction so I don't see how that can be a string.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK it seems a string vector is a c++ thing?
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,542
    I think an important decision is the API used to communicate with the keyboard.
    PDCurses is a possible method PDCurses - Public Domain Curses
    If Linux you might use NCurses instead.

    But, I am guessing this is a Turbo C conio.h user asking the question.

    Edit: I heard of "generic functions" in C++ and other OOP Languages; but, does C support "generic functions"?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    But, I am guessing this is a Turbo C conio.h user asking the question.
    It could also be a Code::Blocks conio.h user asking the question as well :P

    You will probably need to make a linked-list of words in alphabetical order (I'm guessing that you are not allowed to use binary trees?...)

    If I was doing it, I'd make a second list with all possible words until I ended up with a list of one word


    [list] -> hello, hey, harry, world, wait, where
    [second list] -> hello, hey, harry, world, wait, where

    [user types] h

    [second list] -> hello, hey, harry (Note that words that don't fit are deleted from list)

    [user types] e

    [second list] -> hello, hey

    [user types] l

    [second list] -> hello

    Because there is only one option, auto-fill chooses hello.

    Second list is reset and waits for new word.
    Last edited by Click_here; 01-13-2013 at 08:58 PM.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User Dabz's Avatar
    Join Date
    Jan 2013
    Location
    Durham, UK
    Posts
    3
    OK it seems a string vector is a c++ thing?


    Sorry, I missed the "C" part in the thread title, but generally, you need to built a list of something, be it a vector or whatever!

    And yeah, a vector is a dynamic storage container... Real handy things to use, I've wrote this type of setup a few times, when writing editors (Currently writing one now in fact), in fact, I couldnt get my head around the auto-complete feature of AvalonEdit in C#, so, I did my own.

    I think mine and click_here's explanation should get you motoring, it is a puzzle, but its one of them puzzles that once you crack it, you'll be "Wahey"!

    Dabz

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Thanks guys appreciate the help a lot.
    Here's the thing and again sorry for exploiting you lol. My program needs to store one line of my group of words.
    I need to print only one line. ex: hello hey world war weird etc.. then it needs to store these words. Later on when i print for example h it will give the options i wrote earlier. I guess using linked lists like Click_here explained.
    Small question to ask: is it possible to make a string vector in C?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    You can make a variable length char array using malloc. The utility functions for strings can be found in the string.h library.

    My suggestion to you is to learn how to make a linked list as a first step
    Last edited by Click_here; 01-14-2013 at 04:04 PM.
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    The slow, but simple-to-implement method is to check (strstr(str, current)user_input) == str) for every string in the list whenever the user updates the input field. If there's only one match, then auto-complete it, otherwise you might suggest all the possible completions. When I say “list”, I mean any data structure that is capable of representing a simple list of elements. If you know all of the strings before interpretation, you could simply store many static strings into an array of pointers to char that is terminated with a null pointer, then do a linear search through the entire list each time. Slow, as I said, but very simple.

    If you have your eye on optimisation, then a prefix tree (trie) seems like a good data structure for this particular program, since all you need to do is keep track of a single pointer.

    Dynamic Memory, Structs, Data Base, Linked lists and generic functions
    Sounds like crap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little help to complete my program..pls..
    By scorpio76 in forum C Programming
    Replies: 14
    Last Post: 02-24-2011, 04:58 AM
  2. c++ complete program
    By gaza rose in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2008, 12:47 PM
  3. auto log in program ....
    By twomers in forum C++ Programming
    Replies: 15
    Last Post: 01-16-2006, 05:39 AM
  4. Auto Complete ?
    By diediemustdie in forum C Programming
    Replies: 10
    Last Post: 11-18-2004, 12:24 AM
  5. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21