Question

Write in C language

5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark the end of string destination. Write a program to test your You are not allowed to use any function declared in string.h You may write a function which returns the length of a string and use it if you need it. Recall that a string is a char array with the null character marking the end. The length of the string is the number of characters in the array appearing before the null character.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
void string_copy(char source[], char destination[], int n) {
int i;
for(i=0;i<n && source[i] != '\0';i++) {
destination[i] = source[i];
}
destination[i]='\0';
}
int main()
{
char source[50] = "abcdef";
char destination[50];
string_copy(source, destination, 50);
printf("source = %s\t destination = %s\n",source,destination);

return 0;
}

Output:

$gcc -o main *.c
$main
source = abcdef  destination = abcdef
Add a comment
Know the answer?
Add Answer to:
Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...
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
  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • Actually I don't know how to solve this problem? 1-for return type, 2 pts for parameters)...

    Actually I don't know how to solve this problem? 1-for return type, 2 pts for parameters) Provide the prototype for a function called my_strcpy()that accepts two pointers to characters, which represent addresses to the beginning of the destination buffer/array and the source buffer array. The function copies one character at a time from the source buffer into the destination buffer until the null character is encountered. The null character should also be copied over. The function must return a pointer...

  • Cannot use/call any library functions. Write a C function char* my_strcat(char *dest, const char *src) that...

    Cannot use/call any library functions. Write a C function char* my_strcat(char *dest, const char *src) that copies the contest of string src to the end of string dest, assuming that there is enough storage space in dest to accommodate the resulting concatenated string. The function returns dest.

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • **C** Write a C function that inputs a pointer to a string and a pointer to...

    **C** Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype: void tighten(char *oldstring, char* newstring, int length); The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char...

  • pUI) FOU are to write a function which has a prototype: void count (char sl, int *pUpper, "pLower, "poigit, pother) s [ is a character string which may be of any length. Other arguments a...

    pUI) FOU are to write a function which has a prototype: void count (char sl, int *pUpper, "pLower, "poigit, pother) s [ is a character string which may be of any length. Other arguments are address of where the number of upper, lower, digits, and other characters in the string should be placed. For example (this is only an example) the code (put into a properly written program): char all "12145-9ABD, 3f0 :bbB2" char bll "148x3!!" char c[] = {...

  • In the space below, write a C function definition for a function named StrLower, which will...

    In the space below, write a C function definition for a function named StrLower, which will receive one parameter, a pointer to a null-terminated string (i.e., pointer to char), convert each character in the string to lowercase (using the tolower function from <ctype.h>), and return nothing to the caller. Inside the function definition, you may use a for loop or a while loop. If you use any variables other than the incoming parameter, you must define them locally in the...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • No pointers please Urgent!!! Q5. Implement a C program that includes a function that copies one...

    No pointers please Urgent!!! Q5. Implement a C program that includes a function that copies one string into another string. You cannot use string.h library for this exercise. Use the following function prototype: void copystring(chars1 ().char s2[]); A sample run would be as follows: Please enter a string: cngcourse String 1 is copied to String 2 String 1 is cngcourse and String 2 is cngcourse

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