Question

In: Computer Science

Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...

Program in C:

The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.

Solutions

Expert Solution

#include <stdio.h>

#define LIMIT 50//w w w .  j  a va  2 s . com

char * mystrcpy(char *s1, char *s2, int n);
char * get(char *string, int n);
void clear_string(char * string, int n);

int main(void)
{
  char s1[LIMIT];
  char s2[LIMIT];
  int n;

  printf("Enter a string to copy: ");
  get(s2, LIMIT);
  while (s2[0] != '\0'){
    printf("How many characters do you want to copy? (maximum %d) ", LIMIT);
    scanf("%d", &n);

    while (getchar() != '\n')
      continue;

    if (n > LIMIT)
      n = LIMIT;

    printf("Original string: %s\n", s2);
    mystrcpy(s1, s2, n);
    printf("Copy: %s\n", s1);

    clear_string(s1, LIMIT);

    printf("Enter a string to copy (empty line to quit): ");
    get(s2, LIMIT);
  }

  puts("Bye");

  return 0;
}
// copy n characters from s2 to s1, if length of s2 is n or greater then s1 may not be null terminated
char * mystrcpy(char *s1, char *s2, int n){
  int i = 0;
  // copy n characters or until null char
  while (s2[i] != '\0' && i < n){
    s1[i] = s2[i];
    i++;
  }
  // if i < n pad s1 with null character
  while (i < n)
  {
    s1[i] = '\0';
    i++;
  }

  return s1;
}
// wrapper for fgets that replaces first newline with null
char * get(char *string, int n){
  char *return_value = fgets(string, n, stdin);

  while (*string != '\0'){
    if (*string == '\n'){
      *string = '\0';
      break;
    }
    string++;
  }

  return return_value;
}
// replace first n characters in string with nulls
void clear_string(char *string, int n){
  for (int i = 0; i < n; i++)
    string[i] = '\0';
}

The screenshot of code:

Output:

Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......


Related Solutions

write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design...
For exam review: Given a stack S1 with n numbers and an empty stack S2, design an algorithm (and write psudeocode for it) to sort all the numbers (from small on top to large on bottom) and store them in the originally empty stack S2, using only the stack ADT functions with the two given stacks, and a fixed number (independent of n) of int and char variables. What is the time complexity of your algorithm (related to n) in...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
In this C program, the function reverseStr reverses the nth word in a string. If n...
In this C program, the function reverseStr reverses the nth word in a string. If n is larger than the number of words in the string then the function returns 0, else return the function modifies the string and returns 1. Task: write the reverseStr function that follow the requirements below: 1. consistent with the prototype and code below 2. original string must be changed 3. DON'T use functions in string.h Tips: 1. string input to this function is non-empty,...
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
Write a program in C++ that Copies Story.txt to DuplicateStory.txt. Each line in Story.txt will be...
Write a program in C++ that Copies Story.txt to DuplicateStory.txt. Each line in Story.txt will be written three times in DuplicateStory.txt.
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT