Thread: Problem with strings

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Problem with strings

    I need to get strings to be compared semantically, but of course this can only work with strcmp. But I don't know how to compare two strings that aren't the same.
    This is what I want (but of course it doesn't work since you can't compare them with == ! )

    Code:
    #define NWOORDEN	267
    #define NTRIALS                     200
    
    for (i = 0; i < NTRIALS; i++) {
    
    do { int l =ts_rint(NWOORDEN); 
    strcpy(data[i].prime,woord[l].woord);}
    while ( 
    
    (data[i].prime=="chinese" && (data[i].target == "tourist" || data[i].target == "places" || data[i].target == "person" || data[i].target == "germans" || data[i].target == "flemish")) 
    
    || (data[i].prime=="protest" && (data[i].target == "opstand" || data[i].target == "ontslag" || data[i].target == "vakbond" || data[i].target == "sanctie"))
    							
    || (data[i].prime=="climate" && data[i].target == "oerwoud"))
    
    }
    I have a whole lot of words that have to be compared like this !

    Anyone has an idea??

    Thank you !!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I don't see anything here that would prevent you using strcmp()

    You could write another function to compare one word against a list of other words, to simplify your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up a small example perhaps? It's not clear what the problem is.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Problem is that I have 267 words.
    My script will print 2 random words on the screen (for example 'chinese' and 'protest').
    This all works correctly.

    But problem is that I have to tell the script that certain words can't be presented together. (for example 'chinese' and 'tourist)
    So it will have to get another random word if that happens.

    I want to use:
    do ("get a random word ") while ( (1st word == "chinese" &&
    2ndword == "tourist) || (1st word == "chinese" && 2nd word == "places") || ...

    It's a lot of words that can't go together so it's quite a lot of lines of data.
    But of course now that I've done it like this I can see that it doesn't work.

    Reason for that is that you can't compare strings with ==

    But I can't use strcomp() either I think because if I say for example
    do("get a random word") while ((strcomp(1stword,2ndword)== ???)

    What would I have to put where the question marks are? And won't that make sure that other words are also not chosen together(other than I want) ?

    Hope that is a bit more clear

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, you need to build some kind of data structure, like say
    Code:
    struct wordpairs {
      char *word;
      int numAllowed;
      char **allowedPairs;
    };
    Code:
    char *pairsForFred[] = { "Wilma", "Pebbles" };
    char *pairsForBarney[] = { "Betty", "BamBam" };
    struct wordpairs wordlist[] = {
       { "Fred", 2, pairsForFred },
       { "Barney", 2, pairsForBarney }
    }
    Now you could go the other way, for each word, you have a list of words it CAN'T be matched with. Use whichever gives you the shorter lists.

    Having generated the first word, you search for it in wordlist[i].word.
    Then you search for your second word in wordlist[i].allowedPairs[j]





    As for your comparison, strcmp() returns == 0 if they match, and != 0 if they don't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    RTFM. either by google or man page...
    eg
    NAME
    strcmp, strncmp - compare two strings

    SYNOPSIS
    #include <string.h>

    int strcmp(const char *s1, const char *s2);

    int strncmp(const char *s1, const char *s2, size_t n);

    DESCRIPTION
    The strcmp() function compares the two strings s1 and s2. It returns
    an integer less than, equal to, or greater than zero if s1 is found,
    respectively, to be less than, to match, or be greater than s2.
    Edit: yes salem approach is better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM