English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn how to copy a string without using the strcpy() function.
To understand this example, you should understand the followingC language programmingTopic:
As you know, the best way to copy a string is to use the strcpy() function. However, in this example, we will manually copy the string without using the strcpy() function.
#include <stdio.h> int main() { char s1[100], s2[100], i; printf("Input string s1: "; fgets(s1, sizeof(s1), stdin); for (i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); return 0; }
Output Result
Input string s1: Hey fellow programmer. String s2: Hey fellow programmer.
The above program manually copies the string s1Copy the content to the string s2.