(Write Your Own String Comparison Functions) Write two versions of each string-compar- ison function in Fig. 22.21. The first version should use array subscripting, and the second should use pointers and pointer arithmetic.
What will be an ideal response?
```
#include
using namespace std;
int stringCompare1( const char *, const char * );
int stringCompare2( const char *, const char * );
int stringNCompare1( const char *, const char *, unsigned );
int stringNCompare2( const char *, const char *, unsigned );
int main()
{
char string1[ 100 ], string2[ 100 ];
unsigned n = 3; // number of characters to be compared
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';
cout << "\nThe value returned from stringNCompare1(\"" << string1
<< "\", \"" << string2 << "\", " << n << ") is "
<< stringNCompare1(string1, string2, n)
<< "\nThe value returned from stringNCompare2(\"" << string1
<< "\", \"" << string2 << "\", " << n << ") is "
<< stringNCompare2( string1, string2, n ) << endl;
return 0;
} // end main
int stringCompare1( const char *s1, const char *s2 )
{
int sub;
// array subscript notation
for ( sub = 0; s1[ sub ] == s2[ sub ]; sub++ )
; // empty statement
sub--;
if ( s1[ sub ] == '\0' && s2[ sub ] == '\0' )
return 0;
else if ( s1[ sub] < s2[ sub ] )
return -1;
else
return 1;
} // end function stringCompare1
int stringCompare2( const char *s1, const char *s2 )
{
// pointer notation
for ( ; *s1 == *s2; s1++, s2++ )
; // empty statement
s1--;
s2--;
if ( *s1 == '\0' && *s2 == '\0' )
return 0;
else if ( *s1 < *s2 )
return -1;
else
return 1;
} // end function stringCompare2
int stringNCompare1( const char *s1, const char *s2, unsigned n )
{
unsigned sub;
// array subscript notation
for ( sub = 0; sub < n && ( s1[ sub ] == s2[ sub ] ); sub++ )
; // empty body
sub--;
if ( s1[ sub ] == s2[ sub ] )
return 0;
else if ( s1[ sub ] < s2[ sub ] )
return -1;
else
return 1;
} // end function stringNCompare1
int stringNCompare2( const char *s1, const char *s2, unsigned n )
{
// pointer notation
for ( unsigned c = 0; c < n && (*s1 == *s2); c++, s1++, s2++ )
; // empty statement
s1--;
s2--;
if ( *s1 == *s2 )
return 0;
else if ( *s1 < *s2 )
return -1;
else
return 1;
} // end function stringNCompare2
```
Enter two strings: tommy tomato
The value returned from stringCompare1("tommy", "tomato") is 1
The value returned from stringCompare2("tommy", "tomato") is 1
The value returned from stringNCompare1("tommy", "tomato", 3) is 0
The value returned from stringNCompare2("tommy", "tomato", 3) is 0
You might also like to view...
In Publisher, what happens when text does not all fit in a text box?
A) The text size is made smaller. B) A new text box is created automatically for the overflow. C) The text is deleted. D) The text is hidden.
At the __________________ phase you spend a lot of time considering design choices for the projects look and feel.
Fill in the blank(s) with the appropriate word(s).
In Java, polymorphic method binding occurs ____________________ .
a) at run time b) at compile time c) never d) when a programmer writes the code e) during the testing phase of software development
Examples of ____ computers are portable media players and smartphones.
A. handheld B. tablet C. netbook D. mini-laptop