Question

In: Computer Science

Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string...

Question 3: C-Strings and pointers (10 pts)

  1. [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access.

Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.)

// Append strt2 to str1

void my_strcat(char str1[], char str2[])

{

//YOUR CODE HERE  

}

// example of using my_strcat()

#include <stdio.h>

int main(void)

{

char my_str1[50] = “hello ”;

char my_str2[] = “world”;

my_strcat(my_str1, my_str2);

// Printf should print: hello world.

printf(“%s \n”, my_str1);

}

  1. [5] Rewrite the above function, this time using pointer access, i.e. *str, but no array access.

Note: In general, a function’s parameter declarations “char str[]” and “char *str” are equivalent (i.e. they are interchangeable)

// Append strt2 to str1

void my_strcat(char* str1, char* str2)

{

// YOUR CODE HERE  

}

// example of using my_strcat()

#include <stdio.h>

int main(void)

{

char my_str1[50] = “hello ”;

char my_str2[] = “world”;

my_strcat(my_str1, my_str2);

// Printf should print: hello world.

printf(“%s \n”, my_str1);

}

Solutions

Expert Solution

a) [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access.

Ans.

#include <stdio.h>

void my_strcat(char str1[], char str2[])

{
   int c, d;

   c = 0;

    while (str1[c] != '\0') {// Run till str1 is complete
   c++;
    }

    d = 0;

    while (str2[d] != '\0') { // concate to str1
   str1[c] = str2[d];
   d++;
   c++;
   }

    str1[c] = '\0'; // add null to concate str1
}
int main(void)

{

char my_str1[50] = "hello " ;

char my_str2[] = "world";

my_strcat(my_str1, my_str2);

// Printf should print: hello world.

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

}

/* OUTPUT */

b) [5] Rewrite the above function, this time using pointer access, i.e. *str, but no array access.

ans.

#include <stdio.h>

void my_strcat(char* str1, char* str2)

{
   while(*str1)
str1++;

while(*str2)
{
*str1 = *str2;
str2++;
str1++;
}
*str1 = '\0';

}


int main(void)

{

char my_str1[50] = "hello " ;

char my_str2[] = "world";

my_strcat(my_str1, my_str2);

// Printf should print: hello world.

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

}

/* PLEASE UPVOTE */


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.
Recall that a 5-bit string is a bit strings of length 5, and a bit string...
Recall that a 5-bit string is a bit strings of length 5, and a bit string of weight 3, say, is one with exactly three 1’s. a. How many 5-bit strings are there? b. How many 5-bit strings have weight 0? c. How many 5-bit strings have weight 1? d. How many 5-bit strings have weight 2? e. How many 5-bit strings have weight 4? f. How many 5-bit strings have weight 5? g. How many 5-bit strings have weight...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that prompts the user for their party affiliation (Democrat, Republican, or Independent) and responds accordingly with a Donkey,
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
C++ function to a string into enum function to a enum into a string check valid...
C++ function to a string into enum function to a enum into a string check valid / loop ( asking a couple of times until answer invaild) For example Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY) output should be what is your favorit fruit? : strawberry you will have STRAWBERRY. what is your favorite fruite : peach invaild TIA
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT