Comparing two strings with arrays and pointers

A

agent349

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 == *ssPtr ) { //here's the problem...

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

}

else if (s != ss) {

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

}

}
}

return 0;

}
 
K

Kai-Uwe Bux

agent349 said:
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 == *ssPtr ) { //here's the problem...


use

if ( s == ss ) {

and it will compile. However, it will not do what you want.
cout << "The entered strings are equal.\n\n";

}

else if (s != ss) {

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==ss will be announced
as a matching of the strings. Every single s != ss 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 != s[ii] ) {
is_equal = false;
break;
}
}
// now is_equal is true if and only if s==ss 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().
 
I

Ian

agent349 said:
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 == *ssPtr ) { //here's the problem...

should read *sPtr++ == *ssPtr++ - no indexing!
cout << "The entered strings are equal.\n\n";

}

else if (s != ss) {
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
 
A

agent349

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 != ss ) {
is_equal = false;
cout << "The entered strings are not equal.\n";
break;
}
else { cout << "The entered strings are equal.\n"; break; }
}

}

return 0;

}
 
B

B. Gandhi

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 != ss ) {
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.
 
Joined
Mar 10, 2009
Messages
1
Reaction score
0
hey just a quick to related to this topic
what if you have 2d string pointer like a list of names ,
if someone enters last name or first name
it should display the whole thing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top