Question

ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by 10 void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The function should use a loop to copy the contents of src from start to V0 into dest. It should also copy the V0 into dest. This function will fail if dest isnt big enough to hold src, but theres really no easy wayfor us to check for that, so well have to assume that the user uses our function properly.) In main define two character arrays strl and str2 oflength 3l. Prompt the user for a string and use scanf to read the string into the first character amay, strl Then use my strcpy to copy the string from strl into the other char array, str2. Now copy You entered into strl using your my strepy function. Use printf As is n, strl str2) to print both /a.out. Enter a string of length at most 30: Hello World You entered Hello Your programs should be consistent with the example above. Do this part on your own. Write a program named lab11 b.c containing the following function: preconditions src is terminated by Vo dest is big enough hold are postconditions dest contains src and is terminated by 10 void my stropy (char dest, const char src) This funct ion will take two character pointers as parameters. The funct should use a loop to copy the contents of src from start to V0 into dest. It should also copy the 10 into dest. Note that incrementing a pointer makes it point to the next memory location. In main define two character pointers strl and str2 and an integer. Prompt the user for string length use the IO the examples below) and use scanf to store this in your int variable. Use malloc for calloc whichever one your prefer) to allocate memory for both character pointers with the length (plus one) read from the user Make sure that malloc (or calloc returned non-NULL addresses for both character pointers. Then prompt the user for a string (use the lo in the examples below) using scanf to read the string into the first pointer, strl Then, as in Part I, use my strepy to copy the string from strl into the other char pointer, str2. Now copy You entered into strl using your my strcpy O function. Use print f is\n, strl str2) to print both strings. After you complete these steps, then free the memory pointed by strl and str2 to prevent memory leaks. Calendar To Do Notifications Courses
media%2F7c5%2F7c5c808b-6cc6-4ea3-960d-73
Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for lab1:

#include <stdio.h>
//preconditions: src is terminated by '\0'
//               dest is big enough to hold src
//postconditions: dest contains src and is terminated by '\0'
void my_strcpy(char dest[], const char src[])
{
    int i = 0;
    while(src[i] != '\0')
    {
       dest[i] = src[i];
       i++;
    }  
    dest[i] = src[i];  
}
int main()
{
    char str1[31], str2[31];
    printf("Enter a string of length at most 30: ");
    scanf("%s", str1);
    my_strcpy(str2, str1);
    my_strcpy(str1, "You entered:");
    printf("%s %s\n", str1, str2);
}

Here is the code for lab2:

#include <stdio.h>
#include <stdlib.h>
//preconditions: src is terminated by '\0'
//               dest is big enough to hold src
//postconditions: dest contains src and is terminated by '\0'
void my_strcpy(char *dest, char *src)
{
    int i = 0;
    while(*(src + i) != '\0')
    {
       *(dest + i) = *(src + i);
       i++;
    }
    *(dest + i) = *(src + i);
}
int main()
{
    char *str1, *str2;
    int size;
    printf("What is the longest length of a string that you will enter? ");
    scanf("%d", &size);
    str1 = malloc(size + 1);
    str2 = malloc(size + 1);
    if(str1 == NULL || str2 == NULL)
    {
       printf("malloc failed to allocate enough memory!\n");
       return 0;
    }
    printf("Enter a string: ");
    scanf("%s", str1);
    my_strcpy(str2, str1);
    my_strcpy(str1, "You entered:");
    printf("%s %s\n", str1, str2);
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help please this is C/C++ code /* * Lab12, the purpose of this lab is to...

    Help please this is C/C++ code /* * Lab12, the purpose of this lab is to improve your skills in handling c strings * with pointers . * use of : strlen ,string concatenation strcat, compare strcmp, * copy strcpy and search strrchr * */ #include <cstdlib> #include <iostream> #include<cstring> using namespace std; /** * Create a method that prompts the user for only lowercase letters to represent * a name. * Start a Label, then prompt the user to...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • Need help coding these two C programs 1:27 LTE < s3?bucket-uploads&prefix-attach%2Fjl CS 100 Exam Two -Coding...

    Need help coding these two C programs 1:27 LTE < s3?bucket-uploads&prefix-attach%2Fjl CS 100 Exam Two -Coding -Spring 2018 You are not allowed to use the Internet while coding the two problems below. You can log into the cs-intro server to test your programs if you wish When you have finished submit your exam via Blackboard Create a directory called exam2 using mkdir exan2 and move into that dinectory with ed exan2 Complete the I. Name this program one.c-This peogram reads...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT