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!
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!