(Write Your Own String Length Function) Write two versions of function strlen 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;

// prototype
unsigned long stringLength1( const char * );
unsigned long stringLength2( const char * );

int main()
{
char string[ 100 ];

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

cout << "\nAccording to stringLength1 the string length is: "
<< stringLength1( string )
<< "\nAccording to stringLength2 the string length is: "
<< stringLength2( string ) << endl;

return 0; // indicate successful termination
} // end main

// finding string length using arrays
unsigned long stringLength1( const char *sPtr )
{
int length;

// array subscript notation
for ( length = 0; sPtr[ length ] != '\0'; length++ )
; // empty body

return length;
} // end function stringLength1

// finding string length using pointers
unsigned long stringLength2( const char *sPtr )
{
int length;

// pointer notation
for ( length = 0; *sPtr != '\0'; sPtr++, length++ )
; // empty body

return length;
} // end function stringLength2
```
Enter a string: length
According to stringLength1 the string length is: 6
According to stringLength2 the string length is: 6

Computer Science & Information Technology

You might also like to view...

Cite some examples of integrity constraints that you think can apply to the database shown in Figure 1.2.

What will be an ideal response?

Computer Science & Information Technology

Comparison operators include *, ?, and [ ]

Indicate whether the statement is true or false

Computer Science & Information Technology

Where do the brush options appear on the Brush panel?

A. on the right side B. under the Master Diameter slider C. above the New Effects window D. on the left side

Computer Science & Information Technology

As the total loan balance decreases, a(n) ________ proportion is applied to interest and a greater proportion to principal

Fill in the blank(s) with correct word

Computer Science & Information Technology