Thread: Help Writing My Own String Compare

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Question Help Writing My Own String Compare

    what i want to do is to write my own string compare function because the built in doesnt work properly for me.

    my two strings are:
    Code:
    char street_name[MAX][STR_MAX]
    char comp_street_name[MAX][STR_MAX]
    what i want to do is compare the word "Heidelberg" in 'street_name' to the various street names stored in 'comp_street_name' which is a dynamic array and is holding "Linden" in [0], "Heidelberg" in [1], etc.... until the end.

    can anyone help me compare these dynamic arrays with or without the help of strcmp. some code examples would be great help.

    thanx

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    char street_name[MAX][STR_MAX]
    char comp_street_name[MAX][STR_MAX]
    Both of these are arrays of strings. If you want to compare one string against every string in an array of strings one of them should be declared as a single string. Of course you could be trying to compare every string in an array against every string in another array or just comparing one string from an array against all strings in another array.
    To compare a string against all strings in an array of strings would be like this
    Code:
    for (int i=0; i<MAX;i++)
    {
       if (!strcmp(Array[i], FooString))
       //do something
    }
    Last edited by Quantum1024; 04-07-2005 at 02:05 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    so should i move the single string read in of "Heidelberg" into a temp string like FooString in ur example, then compare like u have done above quantim1024?

    im a little unsure what result
    Code:
    if ( !strcmp ( array[i] ...
    will give. shouldnt i compare the result to zero if they r the same?

    and also what do i do because my other string is a dynamic 2D array. should i have
    Code:
    if ( !strcmp ( array[i][j] ...
    . cos i use array[location of word][location of character]. and if that makes a difference then how would i set that out in the for loop?

    thanks, im so confused right now.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by djwicks

    im a little unsure what result
    Code:
    if ( !strcmp ( array[i] ...
    will give. shouldnt i compare the result to zero if they r the same?
    if two strings are the same strcmp returns zero so using the ! (not) operator makes the false experesion become true so if the expersion is true (string matches) we do what follows the if statement. You could also use ==0.

    and also what do i do because my other string is a dynamic 2D array. should i have
    Code:
    if ( !strcmp ( array[i][j] ...
    . cos i use array[location of word][location of character]. and if that makes a difference then how would i set that out in the for loop?
    if you want to start comparing from a certain charactor then you would do this
    Code:
    strcmp(&array[i][j],...
    But if you're always going to be comparing from the start of the word you just need
    Code:
    strcmp(array[i],...
    Last edited by Quantum1024; 04-07-2005 at 09:09 AM.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    ok thanx, ill put some effort in over the weekend and see where i get.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  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. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM