C

How To Copy a String in C Without Using strcpy

In this tutorial, we are going to see how to copy a string in C without using strcpy.
 

How To Copy a String in C Without Using strcpy
#include <stdio.h>

int main()
{
    char i, str1[100], str2[100];

    printf(" Enter a string: ");
    scanf("%s",str1);
  
    for(i = 0; str1[i] != '\0'; ++i)
    {
        str2[i] = str1[i];
    }
    str2[i] = '\0';

    printf(" Original String: %s\n", str1);
    printf(" Copied String: %s\n", str2);
    return 0;
}

Output:

 Enter a string: StackHowTo
 Original String: StackHowTo
 Copied String: StackHowTo

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *