Thread: simple string, search in input

  1. #16
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    \0 is a null character, that's so that it stops trying to convert the string to lower case once it reaches the end of the string.

  2. #17
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Quote Originally Posted by Dweia
    \0 is a null character, that's so that it stops trying to convert the string to lower case once it reaches the end of the string.
    thanks dweia!



    Is there anyone who help can me further with this problem?
    The while for checking for badwords, I now got this part:
    Code:
    char badwords[20][20];
    ifstream file_bwchk ("badwords.txt");
    char temp[20];
    strcpy (temp, chattext);
    int i = 0;
    cout << temp << endl;
    while(file_bwchk >> temp) {
     for(i = 0; i < 20; ++i) {
       if(strncmp(temp, badwords[i]) == 0) {
         cout << "Naughty, naughty" << endl;
       }
     }
    }
    This one gives me this error:
    Code:
    /usr/include/string.h:100: error: too few arguments to function `int
       strncmp(const char*, const char*, unsigned int)'
    I guess I have to use that "unsigned int" somewhere, but I can't exactly figure out where.
    When just putting an int into there without actully doing something with it the console returns "Naughty, naughty" all the time, also when saying "test" or something like that. And it does say it twenty times to, not one time
    It looks like it is reconizing every line of the badwords.txt (which excists of 3 lines or so, not twenty) with the chattext.

    Does anyone know how I have to use that extra int?

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    the third parameter to strncmp is the number of characters you want to compare.

    As discussed earlier strncmp will only match on complete word matches, so you will not find badwords in phrases.
    strstr() is better, but you're probably going to have to write your own comparision routine.

    As far as the logic of your latest code...

    You can either read all the badwords into an array, and then check the chattext against that array.
    (harder because you'll have to dynamically adjust the array size, or at least make the array sufficiently large, and keep a count of how many words you read in)

    or you can loop through the file each time and check each line against chattext
    (bad if doing over and over again as file i/o is slow)

    You currently have a mix of the two methods... with other problems like storing chattext in temp, and then overwriting temp with a line from badwords.txt


    Here's an example of looping through the file and comparing each line to chattext
    It's based off your latest code
    Code:
    	ifstream file_bwchk ("badwords.txt");
    	char badword[20];
    
    	while(file_bwchk >> badword) {
    		if(strncmp(chattext, badword, strlen(badword)) == 0) {
    	            cout << "Naughty, naughty" << endl;
    	            break; //found a match no need to keep checking
    		}
    	}
    an example of storing the words in an array first (these are just to give you an idea... much error checking needed)
    Code:
        ifstream file_bwchk ("badwords.txt");
        char badword[20];
        char badwords[20][20];
        int badword_cnt = 0;
    
        while(file_bwchk >> badword) {
            strcpy(badwords[badword_cnt++], badword);
        }
    
        for(int i=0; i<badword_cnt; i++) {
            if(strncmp(chattext, badwords[i], strlen(badwords[i])) == 0) {
                cout << "Naughty, naughty" << endl;
                break;
    	}
        }
    Last edited by spydoor; 08-09-2005 at 12:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM