I'm trying to add a space before and after a user input. I have the space after using strcat() but cant figure out how to put one before. Here is the snippet.

void find_keyword()
{
    int len=0;
    int lenkey=0;
    int key_no=0;
    char teststr[50];
    while((ip.keyfound==0) && (key_no!=MAX_KEY))
    {
        // getting the length of the keyword
        lenkey=strlen(keys[key_no].getword());

        char *ptr=NULL;
        // Concatenated the user input to include a space. This fixes accidental keyword hits.
        strcat(ip.userip, " ");

        ptr=strstr(ip.userip,keys[key_no].getword());
        if (ptr!=NULL)
        {
            // keyword found !
            ip.keyfound=1;
            ip.keyno=key_no;
            strcpy(ip.keyword,keys[key_no].getword());
            break;
        }

Is userip char*? If yes, then to put a space before it you will have to move current text right one character to open up the byte in which to put the space.

For example, if userip originally contains "255.255.255" then you will call memmove() to shift all characters right one byte

memmove(&userip[1],userinp,strlen(userinp));
userip[0] = ' ';

Warning! userip must be declared large enough to hold the extra characters.

Yes, userip is char.
What it is doing is searching an external file for keywords. At first the program would hit on keywords inside other words:
ex: "no" is a keyword
If I typed "nothing", the program would hit on the keyword "no". I partially fixed this by adding the space after the input by the user with strcat(). Now, keywords in the middle of other words will not hit, but if the keyword is at the end of a word, like "casino", the program will still hit. That is why i need a space before and after the user input.

I tried your solution, but it doesn't seem to be doing anything other than giving me odd bugs such as repeating the same responce three times. I dont know if I am still doing it wrong.

strcat(ip.userip, " ");
memmove(&ip.userip[1],ip.userip,strlen(ip.userip));
ip.userip[0] = ' ';

Thank you for your help.

I know this isn't an answer to your question, but why do you need to do all this adding of spaces and such? It sounds like it could be a bit of a hack? If you don't mind, what is it that you're actually trying to accomplish with the find_keyword function?

its a simple ELIZA-like AI program that you can talk to and it answers you. The keywords are the words that it recognizes and responds to.

The keywords are the words that it recognizes and responds to.

Yes, but why do you have to do all this adding spaces here and there before you can regonise keywords?

If i dont add spaces before and after a keyword, the program will hit on keywords that are inside or at the end of other words. If "no" is a keyword, the program will hit in "nothing" or "casino" just because they have "no" in them.

Sounds like you need to redesign how you are getting yourt words from the file and how you are comparing them. Have you ever used the string object?

Ah, I see. Usually, one would carry out a process of tokenizing the input so that you can analyse each token individually to see if it is a known keyword. You'd do this by taking your input sentence (in your case) and calling some kind of "split" function that breaks the sentence up into individual tokens. Some of these will be keywords and some will be values, etc. You can then look at each token in turn and decide what to do. This way, you will never confuse "no" and "nothing", since strcmp( "no", "nothing" ) != 0

As NathanOliver said, std::string is a good choice here (although some people still prefer char arrays). If you're able to use external libraries, then boost has some useful string-processing algorithms, such as a boost::split, which does exactly what you'd expect it to.

Edited 4 Years Ago by ravenous: Added some comments

Thank you for helping me. I am going to try and re-code the way I check for the keywords.

This article has been dead for over six months. Start a new discussion instead.