English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C program does not use strcpy() to copy a string

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.

Do not use strcpy() to copy a string

#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.

Comprehensive Collection of C Programming Examples