- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to copy string without using strcpy() function
In this section we will see how to copy a string to other string without using strcpy() function. To solve this problem we can write our own function that can act like strcpy(), but here we will follow some trick. We will use another library function to copy a string into another.
The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.
Input − Take one string "Hello World" Output − It will copy that string into another string. "Hello World"
Algorithm
Step 1: Take a string Step 2: Create an empty string buffer to store result Step 3: Use sprintf() to copy the string Step 4: End
Example Code
#include<stdio.h> main() { char str[50]; //create an empty string to store another string char *myString = "Program to copy a String"; sprintf(str, "%s", myString);//Use sprintf to copy string from myString to str printf("The String is: %s", str); }
Output:
The String is: Program to copy a String
- Related Articles
- Write a C program to Reverse a string without using a library function
- Write a C program to convert uppercase to lowercase letters without using string convert function
- What is strcpy() Function in C language?
- Python Program to Calculate the Length of a String Without Using a Library Function
- strcpy() in C/C++
- Python Program to Reverse a String without using Recursion
- C++ program to replace all occurrences of string AB with C without using extra space
- C++ Program to delete an item from the array without using the library function
- C++ Program to create a function without argument and without a return value
- C++ code to count copy operations without exceeding k
- How to disable copy content function using jQuery?
- C++ Program to Copy Strings
- C program to print a string without any quote in the program
- Converting number of corresponding string without using library function in JavaScript
- How to copy from clipboard using tkinter without displaying a window

Advertisements