I have an assignment to take the functions and use array subscripting and pointer arithmetic to write the functions.
int stringLength(const char* s);
char* stringCopy(char* s1, const char* s2);
char* stringConcatenate(char* s1, const char* s2);

and use the following tester driver:

but I have no idea where to start when it comes to writing the functions.

#include <iostream>
using namespace std;

// Implement the following function prototypes
int stringLength1( char *s );
int stringLength2(char *s );
char *stringCopy1( char *s1, const char *s2 );
char *stringCopy2( char *s1, const char *s2 );
char *stringCat1( char *s1, const char *s2 );
char *stringCat2( char *s1, const char *s2 );

// Use the following driver to test implementations:
int main()
{
   char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";
  
   // Test stringLength functions
   cout << "stringLength(" << s2 << "): "
        << stringLength1( s2 ) << endl;
   cout << "stringLength(" << s3 << "): "
        << stringLength2( s3 ) << endl;

   // Test stringCopy functions
   cout << "stringCopy1(s1, " << s2 << "): "
        << stringCopy1( s1, s2 ) << endl;
   cout << "stringCopy2(s1, " << s3 << "): "
        << stringCopy2( s1, s3 ) << endl;
 
   // Test stringCat functions
   cout << "stringCat1(s1, " << s2 << "): "
        << stringCat1( s1, s2 ) << endl;
   cout << "stringCat2(s1, " << s3 << "): "
        << stringCat2( s1, s3 ) << endl;
 
   system("pause");
   return 0; // indicates successful termination
} // end main

Recommended Answers

All 10 Replies

Do you mean that you dont know how to write,call etc. a function?

I know how to write and call a function I guess I just don't understand the assignment. When it comes to arrays and pointers I'm lost...give me an assignment using anything else but my brain scrambles when I have to use arrays and pointers.

i'll do one for you just to get ye' started:

//using pointer arithmetic
int string_length_1(char* cstring)
{
     int size = 0;

     while(*cstring != NULL)
     {
          //increment the pointer
          cstring++;
          size++;
     }

     return size;
}

//Same function using array subscripts
int string_length_2(char cstring[])
{
     int index = 0;

     //using array subscript
     while(cstring[index] != NULL)
     {
          index++;
     }

     return index;
}

your string copy functions will also be just as simple: pass two arrays into your function. use a loop to copy 1 array into another, element by element.

I was about to start working on the other functions but the assignment says I cannot use cstring! Wouldn't it make it much easier if I could?

you are using char pointers... therefore, ye' are using null-terminated character arrays.

trust me.

I tried doing this but get several errors.

#include <iostream>
using namespace std;

//using pointer arithmeticint string_length_1(char* cstring){     int size = 0;      while(*cstring != NULL)     {          //increment the pointer          cstring++;          size++;     }      return size;} //Same function using array subscriptsint string_length_2(char cstring[]){     int index = 0;      //using array subscript     while(cstring[index] != NULL)     {          index++;     }      return --index;}//using pointer arithmetic
int string_length_1(char* s)
{
     int size = 0;

     while(*s != NULL)
     {
          //increment the pointer
          s++;
          size++;
     }

     return size;
}

//Same function using array subscripts
int string_length_2(char s[])
{
     int index = 0;

     //using array subscript
     while(s[index] != NULL)
     {
          index++;
     }

     return --index;
}


int main()
{
	int stringLength1( char *s );
int stringLength2(char *s );

   char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";

   cout << "stringLength(" << s2 << "): "
        << stringLength1( s2 ) << endl;
   cout << "stringLength(" << s3 << "): "
        << stringLength2( s3 ) << endl;

  
 
   system("pause");
   return 0; // indicates successful termination
} // end main

this doesn't fly.. for several reasons.

char* s2 = "education";

try this:

char s2[20] = {'e','d','u','c','a','t','i','o','n', '\0'};

or this:

char* s2 = new char[20];
strcpy(s2, "education");

The main method was provided for us to use to test our code so I don't think I should change it. I guess I would have to change code in the function to correlate with the way main is written?

if someone else gave you the pile of crap known as lines #16 & #17 (or #40 & #41 in your second example) then yes, by all means, take the initiative and change it to something that resembles working c++ code.

I was about to start working on the other functions but the assignment says I cannot use cstring! Wouldn't it make it much easier if I could?

Then you have to rewrite. char* is cstring

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.