Thread: Autocomplete feature prototype

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    27

    Autocomplete feature prototype

    Say you are in chat room and in it there are 3 users.
    paul127, mary532, mark456
    You type pau and press tab to automatically complete paul's user name. The following code prototypes that feature.

    Code:
    /*
    autocomplete.c
    version 0.1
    You know in irc chat room you want to type a user's name quickly, you type a portion of his user name and press tab? This code prototypes that functionality.
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    const char* search1="";
    const char* search2="app";
    const char* search3="apple tree";
    
    char* strings1[3]={"","",""};
    char* strings2[3]={"app","apple treehouse","apple treehouse"};
    
    char* match(const char* search, char** strings, int n) {
       if (n==0) {
          return "";
       }
       if (n==1) {
          if (!strcmp(search, "")) {
             return strings[0];
          }
          else {
             if (!strncmp(strings[0], search, strlen(search))) {
                return strings[0];
             }
             else return "";
          }
       }
       
      
       if (n>1) {
          int i=1;
          int j=n-1;
          int k=1;
          char result[20]="";
          if (!strcmp(search, "")) {
             while (j-- && strings[i]) {
                if (!strncmp(strings[0], strings[i], k)) continue;
                else return "";
                ++i;
             }
             strncpy(result, strings[0], 1);
             result[1]='\0';
             char* r=(char*)malloc(20*sizeof(char));
             strcpy(r, result);
             k++;
             i=1;
             j=n-1;
             while (k<=strlen(strings[0])) {
                while (j-- && strings[i]) {
                   if (!strncmp(strings[0], strings[i], k)) continue;
                   else return r;
                   ++i;
               }
               i=1; j=n-1;
               strncpy(result, strings[0], k);
               result[k++]='\0';
               strcpy(r, result);
             }
             return r;
          }
          if (strlen(search)>=1) {
             int dd[n]; dd[0]=0;
             i=0; j=n; k=strlen(search); int l=0; 
             while (strings[i] && j--) {
                if (!strncmp(strings[i], search, k)) {
                   dd[l++]=i+1;
                }
                ++i;
             } 
             char* s=(char*)malloc(20*sizeof(char));
             i=1; j=l-1; k=strlen(search)+1; 
             if (dd[0]>0) { 
                strcpy(s, search);
                while (k<=strlen(strings[dd[0]-1])) {
                   while (j--) {
                      if (!strncmp(strings[dd[0]-1], strings[dd[i++]-1], k)) continue;
                      else return s;
                   } 
                   j=l-1; i=1; 
                   strncpy(result, strings[dd[0]-1], k);
                   result[k++]='\0';
                   strcpy(s, result);
                }
                return s;
             }
             else return "";
          }
       } 
    }
    
    int main() {
       puts(match(search1, strings2, 3)); // app
       puts(match(search2, strings2, 3)); // app
       puts(match(search3, strings2, 3)); // apple treehouse
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    No..wait...no it doesn't. If you want something like this, check into <termios.h>, which lets you do many things to the basic i/o terminal and streams, including non-canon mode that doesn't require a return (so a '\t' will be read in as soon as it's pressed).

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    Quote Originally Posted by memcpy View Post
    No..wait...no it doesn't. If you want something like this, check into <termios.h>, which lets you do many things to the basic i/o terminal and streams, including non-canon mode that doesn't require a return (so a '\t' will be read in as soon as it's pressed).
    Ok i'll look into it

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    I did coding on galaxy tab. woot

  5. #5
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    Its a nice challenge for me. I'm going to modify it and will tell you. Did you ever make a programme that accept a user name. If user is new it make a file regarding the name of user. After creating a user file, its ask for to set a password. If you say yes, then you will be able to set a password for user and a confirm password again asked by program. And when the confirm happens right then programs ask for login again with user and password to check whether user is authorised or not? I think it will easy for you!

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    previous post
    Please stop being a douchebag.
    I think it will be difficult for you!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C99 feature
    By KIBO in forum C Programming
    Replies: 1
    Last Post: 07-07-2008, 06:17 AM
  2. Autocomplete in visual c++ 6
    By Quantum1024 in forum Tech Board
    Replies: 0
    Last Post: 12-04-2005, 11:39 PM
  3. Best IDE feature
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 01-18-2004, 10:43 PM
  4. VC++6 Troubles autocomplete
    By punkrockguy318 in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2003, 05:47 PM
  5. STL autocomplete and 'helper' in VC++
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-17-2002, 04:43 PM

Tags for this Thread