Thread: string function

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    77

    string function

    is there any function that can return my "Chris" where i enter "Chri" only ???
    i know there is a strcmp function but it will only return mi when all the cmp of the string is the same

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    #include <string.h>

    char chris[5] = { "Chri" };
    strcat(chris, 's');

    http://msdn.microsoft.com/library/de...htm/crt3_9.asp
    Last edited by Dual-Catfish; 05-23-2002 at 06:14 PM.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    or

    strstr()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    strstr() cannot
    im actually looking for a function for a search object
    so if i enter a text into the edit box
    it will also return mi text that is near to the one i entered.
    like when i enter "chris",
    it will look at my database and return mi "Chris"

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    There is no such standard function. You'll have to write your own string parser using strcmp. Probably work in reverse, ie: check through the DB for a match to the first initial, then check through those matches for a match to the first 2 letters, etc. etc. etc. Once you can no longer get a successful match, find the one that matches the closes to your last and return it.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Ahh, theres an example in my book about this. I can attach the files of the Derived control to my post, itt'l have all the code in it to make an autocomplete combo/edit box. Might need a little modifying though.

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Oh, and here's the .h file. I couldn't attach two files to one post

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    I disagree it can be done (if he's trying to do what i think).

    do you have an existing set of records that you are searching through and comparing a string to?

    coz if you are then use strncmp() to compare a certain amount of letters... that should help.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    strstr() will work as it tests for the smaller string in the whole string

    so "Chri" would return a match to "Chris", then test to see if the match is at the begining of "Chris".

    roughly
    Code:
    char        *pString=NULL,sString[64]={"Chris"};
    
    pString=strstr("Chri",sString);
    if(pString!=NULL)//has matched 
       if(*pString==sString[0])//is at begining of string
           SetDlgItemText(hWnd,ID_EDIT,sString);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    thanks everyone for the help
    but can i ask how to i seperate the words into parts so as i can do some coding to check for the nearest, said by jdinger

    will the strstr() work for CString also novacain??
    becoz i see u r using the char pointer and char array

    or is there ways that i can convent the CString to char* or char[]?

  11. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    - You can use the GetBuffer function to get a pointer to the object's character buffer.

    - You can also change the CString with the pointer returned from GetBuffer but you need to release the buffer (ReleaseBuffer) after changing it.

    - You can use the Find or the ReverseFind (only chars) function to find a substring.


    Code:
    CString s("abcdefgd");
    char *ptr = s.GetBuffer(15);
    
    int i = s.Find('d'); /* i = 3 */
    
    i = s.Find('d', 4); /* i = 7 (2th arg is start position) */
    i = s.Find(_T("bc")); /* i = 1 */
    
    if((ptr = strstr(ptr, "bc")) != NULL) /* ptr = "bcdefga" */
    {
       strcpy(ptr, "hello");
       s.ReleaseBuffer(); /* s = "ahelloa" */
    }

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    Originally posted by Monster
    - You can use the GetBuffer function to get a pointer to the object's character buffer.

    - You can also change the CString with the pointer returned from GetBuffer but you need to release the buffer (ReleaseBuffer) after changing it.

    - You can use the Find or the ReverseFind (only chars) function to find a substring.


    Code:
    CString s("abcdefgd");
    char *ptr = s.GetBuffer(15);
    
    int i = s.Find('d'); /* i = 3 */
    
    i = s.Find('d', 4); /* i = 7 (2th arg is start position) */
    i = s.Find(_T("bc")); /* i = 1 */
    
    if((ptr = strstr(ptr, "bc")) != NULL) /* ptr = "bcdefga" */
    {
       strcpy(ptr, "hello");
       s.ReleaseBuffer(); /* s = "ahelloa" */
    }

    i wish to ask wat is the _T for ???

  13. #13
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Sorry, you don't need that. It's for mapping/converting strings from Single Byte Strings to Multi Byte Strings (I think).

    Just forget the _T.
    Cheers, Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM