1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ compare strings program using pointers

  1. Mar 5, 2012 #1
    1. The problem statement, all variables and given/known data
    Create a function called isEqual(). isEqual() takes two strings and determines if the strings are identical. isEqual (s1, s2) should be the same as s1 = = s2

    Code (Text):

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    bool isEqual(int *s, int *st, int x);
    int _tmain(int argc, _TCHAR* argv[])
    {
        int string1[5] = {1, 2, 3, 4, 5};
        int string2[5] = {1, 2, 3, 4, 5};
        for(int i = 0; i < 5; i++)
        {
            cout << string1[i];
        }
        cout << endl;
        for(int h = 0; h < 5; h++)
        {
         cout << string2[h];
        }
        cout << endl;
        isEqual(string1, string2, 5);
        if(isEqual)
        {
        cout << "The two strings are not the same." << endl;
        }
        else
        {
        cout << "The two strings are the same." << endl;
        }
        system("pause");
        return 0;
    }
    bool isEqual(int *s, int *st, int x)
    {
        bool same = true;
        for(int i = 0; i < x; i++)
        {
            same = true;
            if(s == st)
            {
            same = true;
            }
            else
            {
            same = false;
            break;
            }
        s++;
        st++;
        }
        if(same == true)
        {
        return true;
        }
        else
        {
        return false;
        }
    }
     
    Sorry if my code is overly confusing for what it's supposed to do.

    What I want it to do is paste the two strings then it goes to the function compares then, returns true or false and pastes whether they are the same or not. Howver it's not reading any of them the same. Anyone know where I'm going wrong?
     
  2. jcsd
  3. Mar 5, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    That should be:
    Code (Text):

        if(*s == *st)
     
    Also, there is no need to keep setting same=true, you only need to do that one time, only changing it to false if there is a mis-compare.
     
  4. Mar 5, 2012 #3
    I fixed that but it still doesn't work right, it still says the strings are not the same
     
  5. Mar 5, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    update - I updated this post.

    That should be:
    Code (Text):

        if(isEqual(string1, string2, 5) != true)
     
     
    Last edited: Mar 5, 2012
  6. Mar 5, 2012 #5
    I get what you did, though my teacher said something about you can shorten it to just isEqual under a certain circumstance, am I misusing that and do you know when it can be done?
     
  7. Mar 5, 2012 #6

    rcgldr

    User Avatar
    Homework Helper

    The first issue is if(...) is the same as if(0 != ...), so you had the sense of the if reversed in your original code. I'm not sure when you can just use isEqual. One issue with C++ is that you can have multiple functions with the same name isEqual, where the only diference is the parameters, for example:

    bool isEqual(char *, char *, int); // compare two arrays of characters
    bool isEqual(int *, int *, int); // compare two arrays of integers
    bool isEqual(double *, double *, int); // compare two arrays of doubles
    bool isEqual(char *, char*); // compare two strings terminated by zero
    bool isEqual() // always return true if no parameters

    "isEqual" by itself means the address of a function. Normally only used when assigning a pointer to function. Although you can overload functions, I don't think you can overload pointer to functions, so the pointer type determines which function to point to:

    bool ((*pointertofunctionarrayofchar)(char *, char *, int) = isEqual;
    bool ((*pointertofunctionarrayofdouble)(double *, double *, int) = isEqual;
     
    Last edited: Mar 5, 2012
  8. Mar 5, 2012 #7
    Okay, and if I'm understanding you right so then it would be if(isequal) would in a sense mean if(isequal = true) and then I'd have they are the same else they are not the same?
     
  9. Mar 5, 2012 #8

    rcgldr

    User Avatar
    Homework Helper

    isEqual is the address of the function, not it's previous return value. If the function isEqual() is stored in memory starting at 0x411400, then your if statement is the same as if(0x411400 == true).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ compare strings program using pointers
  1. Comparing Arrays (Replies: 2)

Loading...