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...
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];...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs are often executed using command lines arguments. For example: g++ -o cm03_la01 cm03_la01.c. In C such program can be develop using a standard for which the main program has two parameters as follows: int main (int argc, char * argv[ ]) { /*program here */ } 3 Because of the equivalence between pointers and arrays, the above C code can also be written as...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT