(Write Your Own String Copy and Concatenation Functions) Write two versions of each string-copy and string-concatenation function in Fig 22.21. The first version should use array sub- scripting, and the second should use pointers and pointer arithmetic.

What will be an ideal response?


```
#include
using namespace std;

// function prototypes
char *stringCopy1( char *, const char * );
char *stringCopy2( char *, const char * );
char *stringNCopy1( char *, const char *, unsigned );
char *stringNCopy2( char *, const char *, unsigned );
char *stringCat1( char *, const char * );
char *stringCat2( char *, const char * );
char *stringNCat1( char *, const char *, unsigned );
char *stringNCat2( char *, const char *, unsigned );

int main()
{
int n = 4;
char string1[ 100 ];
char string2[ 100 ];

cout << "Enter a string: ";
cin >> string2;

// copy string2 into string1 using different functions
cout << "Copied string returned from stringCopy1 is "
<< stringCopy1( string1, string2 )
<< "\nCopied string returned from stringCopy2 is "
<< stringCopy2( string1, string2 );

cout << "\nCopied " << n << " elements returned from stringNCopy1 is "
<< stringNCopy1( string1, string2, n )
<< "\nCopied " << n << " elements returned from stringNCopy2 is "
<< stringNCopy2(string1, string2, n);

cout << "\nConcatenated string returned from stringCat1 is "
<< stringCat1( string1, string2 )
<< "\nConcatenated string returned from stringCat2 is "
<< stringCat2( string1, string2 );

cout << "\nConcatenated string returned from stringNCat1 is "
<< stringNCat1( string1, string2, n )
<< "\nConcatenated string returned from stringNCat2 is "
<< stringNCat2( string1, string2, n ) << endl;
return 0; // indicates successful termination
} // end main

// copying string with array subscripting
char *stringCopy1( char *s1, const char *s2 )
{
// loop
for ( int sub = 0; s1[ sub ] = s2[ sub ]; sub++ )
; // empty body

return s1;
} // end function stringCopy1

// copying string with pointers and pointer arithmetic
char *stringCopy2( char *s1, const char *s2 )
{
char *ptr = s1;

for ( ; *s1 = *s2; s1++, s2++ )
; // empty body

return ptr;
} // end function stringCopy2

// string copy using aray
char *stringNCopy1( char *s1, const char *s2, unsigned n )
{
unsigned c;

for ( c = 0; c < n && ( s1[ c ] = s2[ c ] ); c++ )
; // empty body

s1[ c ] = '\0';
return s1;
} // end function stringNCopy1

// string copy using pointers
char *stringNCopy2( char *s1, const char *s2, unsigned n )
{
char *ptr = s1;

for ( unsigned c = 0; c < n; c++, s1++, s2++ )
*s1 = *s2;

*s1 = '\0';
return ptr;
} // end function stringNCopy2

// string concatenation using arrays
char *stringCat1( char *s1, const char *s2 )
{
int x;

for ( x = 0; s1[ x ] != '\0'; x++ )
; // empty body

for ( int y = 0; s1[ x ] = s2[ y ]; x++, y++ )
; // empty body

return s1;
} // end function stringCat1

// string concatenation using pointers
char *stringCat2( char *s1, const char *s2 )
{
char *ptr = s1;

for ( ; *s1 != '\0'; s1++ )
; // empty body

for ( ; *s1 = *s2; s1++, s2++ )
; // empty body

return ptr;
} // end function stringCat2

// another version of string concatenation using arrays
char *stringNCat1( char *s1, const char *s2, unsigned n )
{
int x;

for ( x = 0; s1[ x ] != '\0'; x++ )
; // empty body

for ( unsigned y = 0; y < n && ( s1[ x ] = s2[ y ] ); x++, y++ )
; // empty body

s1[ x ] = '\0';
return s1;
} // end function stringNCat1

// another form of string concatenation using pointers
char *stringNCat2( char *s1, const char *s2, unsigned n ) {
char *ptr = s1;

for ( ; *s1 != '\0'; s1++ )
; // empty body

for ( unsigned c = 0 ; c < n && ( *s1 = *s2 ); s1++, s2++ )
; // empty body

*s1 = '\0';
return ptr;
} // end function stringNCat2
```
Enter a string: coo
Copied string returned from stringCopy1 is coo
Copied string returned from stringCopy2 is coo
Copied 4 elements returned from stringNCopy1 is coo
Copied 4 elements returned from stringNCopy2 is coo
Concatenated string returned from stringCat1 is coocoo
Concatenated string returned from stringCat2 is coocoocoo
Concatenated string returned from stringNCat1 is coocoocoocoo
Concatenated string returned from stringNCat2 is coocoocoocoocoo

Computer Science & Information Technology

You might also like to view...

Every presentation theme includes ________ that determine the look of headings and body text

Fill in the blank(s) with correct word

Computer Science & Information Technology

Which of the following tools can perform a ping sweep?

a. Traceroute b. Nslookup c. Angry IP Scanner d. Nmap

Computer Science & Information Technology

A(n) __________ is a plan for conducting a meeting.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Is a flowchart an effective documentation technique for identifying who or what performs a particular task? Explain.

What will be an ideal response?

Computer Science & Information Technology