Use strlen(userStr) to allocate exactly enough memory for newStr to hold the string in userStr (Hint: do NOT just allocate a size of 100 chars).
#include
#include
#include
int main(void) {
char userStr[100] = "";
char* newStr = NULL;
strcpy(userStr, "Hello friend!");
/* Your solution goes here */
strcpy(newStr, userStr);
printf("%s\n", newStr);
return 0;
}

Respuesta :

Answer:

Following are the complete code to the given question:

#include <stdio.h>//header file

#include <string.h>//header file

#include <stdlib.h>//header file

int main()//main method  

{

  char userStr[100] = "";//defining a char array  

  char* newStr = NULL;//defining a char pointer  

  strcpy(userStr, "Hello friend!");//use strcpy method that holds char value  

  newStr = (char *)malloc(strlen(userStr));//defining a char variable that use malloc method

  strcpy(newStr, userStr);//use strcpy method that holds newStr and userStr value

  printf("%s\n", newStr);//print newStr value

  return 0;

}

Output:

Hello friend!

Explanation:

In this code inside the main method a char array "userStr" and a char type pointer variable "newStr" holds a value that is null, and inside this strcpy method is declared that holds value in the parameters.

In this, a malloc method is declared that uses the copy method to hold its value and print the newStr value.