Write a program to copy the source string into the destination string in c

20 Sep 2022 Balmiki Mandal 0 C Programming

C Program to Copy a Source String to a Destination String

Copy the source string into the destination string" means to make a copy of the characters in the source string and store them in the destination string so that the contents of the destination string are identical to the contents of the source string.

Write a c program to copy the source string into the destination string using for loop

#include
void main()
{
    int s[10],d[10];
    int i;
    printf("Enter the string:");
    scanf("%s",s);
    printf("Entered string is:%s\n",s);
    for(i=0;s[i];i++)
    d[i]=s[i];
    printf("s=%s  d=%s\n",s,d);

}

Output

Enter the string: ELECTRO4u
Entered string is : ELECTRO4u
s=ELECTRO4u  d=ELECTRO4u

C program to delete some characters in a given string:

C Programming
 
#include <stdio.h>
#include <string.h>

void delete_characters(char *str, int start, int end) {
  int i;

  for (i = start; i <= end; i++) {
    str[i] = str[i + 1];
  }

  str[end] = '\0';
}

int main() {
  char str[100];
  int start, end;

  printf("Enter a string: ");
  scanf("%s", str);

  printf("Enter the start and end positions of the characters to delete: ");
  scanf("%d %d", &start, &end);

  delete_characters(str, start, end);

  printf("The string after deleting the characters is: %s\n", str);

  return 0;
}

This program works by first prompting the user to enter a string and the start and end positions of the characters to delete. It then uses the delete_characters() function to delete the specified characters from the string. The delete_characters() function works by shifting the remaining characters in the string to the left to fill in the gaps left by the deleted characters.

Output

Enter a string: Hello, world!
Enter the start and end positions of the characters to delete: 7 11
The string after deleting the characters is: Hello, !

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

Design a function to find string length in c

Design a Function to print the string character by character

Write a c program to delete some characters in given string 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.