Question

In: Computer Science

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

Implement two functions in C language:

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

stringCopy() - Copies one string onto another using pointers

#include<stdio.h>

#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 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 MAX_STR_LEN

// TO DO:

// Complete this function!

// YOU ARE NOT ALLOWED TO USE FUNCTIONS FROM THE STRING LIBRARY

}

void stringCopy(char *destStr, char *srcStr)

{

// This function copies the contents of a string whose address

// is given by the pointer *srcStr to the string whose address

// is given by the pointer *destStr.

// Note that the function should never try to copy more than

// MAX_STR_LEN characters

// TO DO:

// Complete this function!

// YOU ARE NOT ALLOWED TO USE FUNCTIONS IN THE STRING LIBRARY

}

int main()

{

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

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

int len1,len2;

len1=0;

len2=0;

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

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

// DO NOT CHANGE ANYTHING ABOVE THIS LINE for main()

// In the space below, complete the code for each step

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

// corresponding strings (complete the stringLength()

// 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 of sand,'

// complete the function stringCopy() for this part

// DO NOT CHANGE ANYTHING BELOW THIS LINE

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

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

return 0;

}

Solutions

Expert Solution

#include<stdio.h>

#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 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 MAX_STR_LEN
// TO DO:
// Complete this function!
// YOU ARE NOT ALLOWED TO USE FUNCTIONS FROM THE STRING LIBRARY
    int count = 0;
    while (str[count]) {
        count++;
    }
    *len = count;
}

void stringCopy(char *destStr, char *srcStr) {
// This function copies the contents of a string whose address
// is given by the pointer *srcStr to the string whose address
// is given by the pointer *destStr.
// Note that the function should never try to copy more than
// MAX_STR_LEN characters
// TO DO:
// Complete this function!
// YOU ARE NOT ALLOWED TO USE FUNCTIONS IN THE STRING LIBRARY
    int i = 0;
    while (srcStr[i]) {
        destStr[i] = srcStr[i];
        i++;
    }
}

int main() {
    char string1[MAX_STR_LEN] = "To see a world in a grain of sand,";
    char string2[MAX_STR_LEN] = "And a heaven in a wild flower.";
    int len1, len2;
    len1 = 0;
    len2 = 0;
    printf("%s\n", string1);
    printf("%s\n", string2);
// DO NOT CHANGE ANYTHING ABOVE THIS LINE for main()
// In the space below, complete the code for each step
// 1 - Update len1 and len2 with the length of the
// corresponding strings (complete the stringLength()
// function for this!)
    stringLength(string1, &len1);
    stringLength(string2, &len2);
// 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 of sand,'
// complete the function stringCopy() for this part
    stringCopy(string2, string1);
// DO NOT CHANGE ANYTHING BELOW THIS LINE
    printf("%s\n", string1);
    printf("%s\n", string2);
    return 0;
}

Related Solutions

C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT