Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...

80.2K

Verified Solution

Question

Programming

Implement two functions in C language:

stringLength() - Takes a pointer to a string, and a pointer toan int variable. It computes the length of the string and updatesthe int variable with the length.

stringCopy() - Copies one string onto another using pointers

#include

#define MAX_STR_LEN 2048

void stringLength(char *str,int *len)

{

// This function computes the length of the string

// at the address in the pointer *str. Once thelength

// has been determined, it updates the length

// variable OUTSIDE this function whose address is

// given by the pointer *len

//

// Note - the length can not be more thanMAX_STR_LEN

// TO DO:

// Complete this function!

// YOU ARE NOT ALLOWED TO USE FUNCTIONS FROM THE STRINGLIBRARY

}

void stringCopy(char *destStr,char *srcStr)

{

// This function copies the contents of a string whoseaddress

// is given by the pointer *srcStr to the string whoseaddress

// is given by the pointer *destStr.

// Note that the function should never try to copy morethan

// MAX_STR_LEN characters

// TO DO:

// Complete this function!

// YOU ARE NOT ALLOWED TO USE FUNCTIONS IN THE STRINGLIBRARY

}

int main()

{

char string1[MAX_STR_LEN]=\"To see a world in agrain of sand,\";

char string2[MAX_STR_LEN]=\"And a heaven in awild flower.\";

int len1,len2;

len1=0;

len2=0;

printf(\"%s\n\",string1);

printf(\"%s\n\",string2);

// DO NOT CHANGE ANYTHING ABOVE THIS LINE formain()

// In the space below, complete the code for eachstep

// 1 - Update len1 and len2 with the length of the

// corresponding strings (complete thestringLength()

// function for this!)

// DO NOT CHANGE THE PRINT STATEMENTS BELOW

printf(\"String 1 is %d characters long\n\",len1);

printf(\"String 2 is %d characters long\n\",len2);

// 2 - Copy string1 to string2 (after you do this,string2

// should contain 'To see a world in a grain ofsand,'

// complete the function stringCopy() for this part

// DO NOT CHANGE ANYTHING BELOW THIS LINE

printf(\"%s\n\",string1);

printf(\"%s\n\",string2);

return 0;

}

Answer & Explanation Solved by verified expert
3.7 Ratings (601 Votes)
include define MAXSTRLEN 2048 void stringLengthchar str int len This function computes the length of the string at the address in the pointer str Once the length has been determined it updates the length variable OUTSIDE this function whose address is given by the pointer len Note the length can not be more than MAXSTRLEN TO DO    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students