473,583 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing two strings with arrays and pointers

First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing the characters.

Here's what I have so far... Thanks for any tips you can provide!

_______________ _______________ _______________ _________
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {

char s[10]; //array 1
char *sPtr; //pointer 1

char ss[10]; //array 2
char *ssPtr; //pointer 2

for (int c=1; c<=3; c++) { //looping 3 times

cout << "Enter first string of 10 characters:";
cin >> s;

cout << "Enter second string of 10 characters:";
cin >> ss;

for (int i=0; i<10; ++i) { //loop through each element

if ( *sPtr[i] == *ssPtr[i] ) { //here's the problem...

cout << "The entered strings are equal.\n\n";

}

else if (s[i] != ss[i]) {

cout << "The entered strings are not equal.\n";

}

}
}

return 0;

}
Jul 22 '05 #1
4 10525
agent349 wrote:
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing the characters.

Here's what I have so far... Thanks for any tips you can provide!

_______________ _______________ _______________ _________
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {

char s[10]; //array 1
char *sPtr; //pointer 1

char ss[10]; //array 2
char *ssPtr; //pointer 2

for (int c=1; c<=3; c++) { //looping 3 times

cout << "Enter first string of 10 characters:";
cin >> s;

cout << "Enter second string of 10 characters:";
cin >> ss;

for (int i=0; i<10; ++i) { //loop through each element

if ( *sPtr[i] == *ssPtr[i] ) { //here's the problem...
use

if ( s[i] == ss[i] ) {

and it will compile. However, it will not do what you want.

cout << "The entered strings are equal.\n\n";

}

else if (s[i] != ss[i]) {

cout << "The entered strings are not equal.\n";

}

}
}

return 0;

}

There are the following problems with your code:

a) The pointers sPtr and ssPtr are not initialized. It would be pure
conicidence if sPtr pointed to the beginning of s. Likewise for ssPtr.

b) During your for loop every instace of s[i]==ss[i] will be announced
as a matching of the strings. Every single s[i] != ss[i] will be
(correctly) claimed to prove the two strings unequal.

c) the strings are compared beyond their ends. If the user does not
input exaclty 10 letters, the results will depend on uninitialized memory.
One correct way to compare two arrays of length 10 is the following idiom:

bool is_equal ( true );
for ( i = 0; i < 10; ++i ) {
if ( s[i] != s[ii] ) {
is_equal = false;
break;
}
}
// now is_equal is true if and only if s[i]==ss[i] for all i < 10.
But you really want to compare strings. Thus, let me suggest:

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main() {

string s;
string ss;

for (int c=1; c<=3; c++) { //looping 3 times

cout << "Enter first string:";
cin >> s;

cout << "Enter second string:";
cin >> ss;

if ( s == ss ) {
cout << "The entered strings are equal.\n";
} else {
cout << "The entered strings are not equal.\n";
}
}
return 0;
}

If you want to insist on the strings not exceeding 10 characters, you
can check for that after input with s.length() and ss.length().
Jul 22 '05 #2
Ian
agent349 wrote:
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing the characters.

Here's what I have so far... Thanks for any tips you can provide!

_______________ _______________ _______________ _________
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {

char s[10]; //array 1
char *sPtr; //pointer 1

char ss[10]; //array 2
char *ssPtr; //pointer 2
May as well make pointers const char*.
for (int c=1; c<=3; c++) { //looping 3 times

cout << "Enter first string of 10 characters:";
cin >> s;

cout << "Enter second string of 10 characters:";
cin >> ss;
Pointers should be assigned here.
for (int i=0; i<10; ++i) { //loop through each element

if ( *sPtr[i] == *ssPtr[i] ) { //here's the problem...
should read *sPtr++ == *ssPtr++ - no indexing!
cout << "The entered strings are equal.\n\n";

}

else if (s[i] != ss[i]) {
Eh?
cout << "The entered strings are not equal.\n";
You should compare until you reach the end of a string, or a difference
before printing anything. }

}
}

return 0;

}


Why not use std::string and save yourself a lot of arse ache?

Ian
Jul 22 '05 #3
Here's what I came up with...seems to do what I need. Thanks for the help!

-------------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main() {

//declare first array
char s[10];
//declare second array
char ss[10];

//loop 3 times
for (int c=1; c<=3; c++) {

cout << "Enter first string:";
cin >> s;

cout << "Enter second string:";
cin >> ss;

bool is_equal ( true );
for (int i = 0; i < 10; ++i ) {
if ( s[i] != ss[i] ) {
is_equal = false;
cout << "The entered strings are not equal.\n";
break;
}
else { cout << "The entered strings are equal.\n"; break; }
}

}

return 0;

}
Jul 22 '05 #4
ag******@yahoo. com (agent349) wrote in message news:<e2******* *************** ****@posting.go ogle.com>...
Here's what I came up with...seems to do what I need. Thanks for the help!

-------------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main() {

//declare first array
char s[10];
//declare second array
char ss[10];

//loop 3 times
for (int c=1; c<=3; c++) {

cout << "Enter first string:";
cin >> s;

cout << "Enter second string:";
cin >> ss;

bool is_equal ( true );
for (int i = 0; i < 10; ++i ) {
if ( s[i] != ss[i] ) {
is_equal = false;
cout << "The entered strings are not equal.\n";
break;
}
else { cout << "The entered strings are equal.\n"; break; }
}

}

return 0;

}


If you want strings of precisely 10 characters, you need to use
s[11] and ss[11] rather than 10 at both places to account for
the '\0' character.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7587
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can someone explain why Python allows comparisons between integers and strings, and how it handles those cases? Why is "1" > 1? Pychecker does not warn...
11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
3
2024
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it always adds things up when you didn't select them) so i only added 2 of the items until I got it worked out. here's the page source (you can look at it...
5
3744
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: ...
36
2812
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
2859
by: Reg Rat | last post by:
Hello all, I'm led to believe that the .Net framework offers intelligent string comparison such that é follows e in an alphabetic sort rather than appearing after z, for example. Does this mean that there is a mechanism for comparing strings that may or may not have accents and having them shown as identical (e.g. comparing Herve with Hèrvé...
4
1914
by: rossum | last post by:
I was comparing strings, as one does: stringA.CompareTo(stringB) == -1 Then I thought that this was not as easy to read as it could be, wouldn't it be clearer to write: stringA < stringB Of course that doesn't compile since the < operator is not defined
4
5999
by: eoghan.kenny | last post by:
Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in a database and always increments. A non-null timestamp is type Binary(8) in sql server and I pass it to a byte array in C#. To compare the two byte...
19
3820
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that...
7
1416
by: arnuld | last post by:
On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote: Either you are not understanding me or I am not able to understand this <p_strcmpthing. This is your code:
0
7827
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8195
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5375
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.