C++ compare strings program using pointers

That is probably not what you want. The function isEqual() returns a value of type bool (either true or false). That value is not stored in the function's name, but rather in a temporary location, often called the "return value" location or register. So you could write:bool a;a = isEqual(x,y,5); // call isEqual and save the return value in variable aif(a == true) // if a is trueif(a) // if a is trueif(!a) // if a is falseif(isEqual(x,y,5)) // if the return value from isEqual is trueif(!isEqual(x,y,5)) // if the return value from isEqual is falseIn summary
  • #1
testme
68
0

Homework Statement


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:
#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?
 
Physics news on Phys.org
  • #2
Code:
    if(s == st)
That should be:
Code:
    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.
 
  • #3
I fixed that but it still doesn't work right, it still says the strings are not the same
 
  • #4
update - I updated this post.

Code:
isEqual(string1, string2, 5);
	if(isEqual)
That should be:
Code:
    if(isEqual(string1, string2, 5) != true)
 
Last edited:
  • #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?
 
  • #6
testme said:
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?
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:
  • #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?
 
  • #8
testme said:
if(isEqual == true)
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).
 

1. How do I compare two strings in a C++ program using pointers?

To compare two strings using pointers in C++, you can declare two char pointers and use the strcmp() function from the library. This function compares the values of the strings pointed to by the two pointers and returns 0 if they are equal, a negative integer if the first string is less than the second, and a positive integer if the first string is greater than the second.

2. Can pointers be used to compare strings with different lengths?

Yes, pointers can be used to compare strings with different lengths. The strcmp() function will compare the strings until it reaches the end of the shorter string, and then return a result based on the remaining characters in the longer string.

3. How do I declare a string using pointers in C++?

To declare a string using pointers in C++, you can declare a char pointer and assign it the address of the first character in the string using the & operator. For example, char *str = &"Hello";

4. Can I use the == operator to compare strings using pointers in C++?

No, the == operator cannot be used to compare strings using pointers in C++. This operator only compares the addresses of the two pointers, not the values they are pointing to. It is recommended to use the strcmp() function for string comparisons in C++.

5. Are there any other string comparison functions available in C++ besides strcmp()?

Yes, there are other string comparison functions available in C++, such as strncmp() and strcasecmp(). These functions have similar functionality to strcmp() but may differ in how they handle special characters or case sensitivity. You can refer to the library documentation for more information on these functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
895
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
824
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top