how to copy a string in c without using strcpy

Solutions on MaxInterview for how to copy a string in c without using strcpy by the best coders in the world

showing results for - "how to copy a string in c without using strcpy"
Klara
05 Feb 2020
1//copy a string without using strcpy in C
2#include <stdio.h>
3int main() {
4    char s1[100], s2[100], i;
5    printf("Enter string s1: ");
6    fgets(s1, sizeof(s1), stdin);
7
8	/*or we can use scanf("%s",s1); instead of fgets*/
9
10    for (i = 0; s1[i] != '\0'; ++i) {
11        s2[i] = s1[i];
12    }
13
14    s2[i] = '\0';
15    printf("String s2: %s", s2);
16    return 0;
17}
18