Second Time In Need Of C++ Help!

NomisST

Member
May 4, 2002
191
0
0
I wonder if anyone remembers me, but I needed to write a program that listed triangles next to each other, well after a few weeks past I'm now doing pointers. I have finished the lab assignment #1, but now I have to do a follow-up which is changing the program to do more things. I am completely confused about how to do the follow-up, I know your supposed to make it like it was a strncmp function, but I don't understand how I would change my stringcompare functions to make it work.

The instructions for the follow-up are as follows:

Modify your string compare functions to take a third argument specifying the number of characters in each
string to compare. These functions should act like function strncmp. Again, one version should use array
subscripting, and the other should use pointers and pointer arithmetic.

This is my original program:

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

int stringCompare1( const char[100], const char[100] );
int stringCompare2( const char *, const char * );

int main()
{
char string1[ 100 ], string2[ 100 ];
cout << "Enter two strings: ";
cin >> string1 >> string2;
cout << "The value returned from stringCompare1( \"" << string1
<< "\", \"" << string2 << "\" ) is "
<< stringCompare1( string1, string2 )
<< "\nThe value returned from stringCompare2( \"" << string1
<< "\", \"" << string2 << "\" ) is "
<< stringCompare2( string1, string2 ) << '\n';
return 0;
}

int stringCompare1( const char s1[100], const char s2[100] )
{
int index;
for ( index = 0; s1[ index ] == s2[ index ] && s1[ index ] != '\0' || s2[ index ] != '\0'; index++ )
;
if ( s1[ index ] == '\0' && s2[ index ] == '\0' )
return 0;
if ( s1[ index ] > s2[ index ] )
return 1;
else
return -1;
}

int stringCompare2( const char *s1, const char *s2 )
{
for ( ; *s2 == *s1 && *s1 != '\0' || *s2 != '\0'; s1++, s2++ )
;
if ( *s1 == '\0' && *s2 == '\0' )
return 0;
else if ( *s1 > *s2 )
return 1;
else
return -1;
}

Now I tried to figure this out for the past 3 days, but still can't figure, it is do Sunday at 6:00PM eastern time so if anyone would lend me a helping hand I would appreciate it. Also I need some big help in C++ for my mid-term does anyone know where I could look for good reviews online or a good book to buy because the book im using isn't helping me understand that well. Thank you to all who reply. Much appreciated!
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
for ( ; *s2 == *s1 && *s1 != '\0' || *s2 != '\0'; s1++, s2++ )
I believe you mean for ( ; (*s2 == *s1) && (*s1 != '\0') && (*s2 != '\0'); s1++, s2++ )

edit: oops fixed
 

NomisST

Member
May 4, 2002
191
0
0
The program works the same either way, I need help with the follow-up, but thanks for the reply.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
can you say exactly what is the problem you are having? your stringcampire2 looks like what the follow up requires so I'm guessing over here.

what you already have might work but the logic is wrong:
for ( ; *s2 == *s1 && *s1 != '\0' || *s2 != '\0'; s1++, s2++ )
;

consider the strings:
s1 = 'a' 'b' '\0'
s2 = 'a' 'b' 'c' 'd' 'e' '\0'

on the third iteration, *s1 = 0, *s2 = c, you have false && false || true = true (&& takes precedence) which still passes, so on the forth iteration it'll access memory beyond the \0 for s1. this doesn't crash for you because your string arrays are the same size.
 

NomisST

Member
May 4, 2002
191
0
0
yea now that i look at it the logic seems incorrect. I'll change that, but that's for the assignment one. I said above that what I need help in is this:

Modify your string compare functions to take a third argument specifying the number of characters in each
string to compare. These functions should act like function strncmp. Again, one version should use array
subscripting, and the other should use pointers and pointer arithmetic.

How do I change my program to do that?