C Program to Copy a String Using Custom Function
This C program demonstrates how to create a custom function 'my_strcpy' that copies a string from one location to another. It takes user input for the source string, handles trailing newlines, and uses the custom function to copy the string, finally printing the copied result.unknown
c_cpp
a month ago
827 B
2
Indexable
Never
#include <stdio.h> // Function to copy a string from src to dest void my_strcpy(char *dest, const char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; // Null terminate the destination string } int main() { char src[100], dest[100]; // Taking user input for source string printf("Enter a string to copy: "); fgets(src, sizeof(src), stdin); // Removing trailing newline from the input (if present) int len = 0; while (src[len] != '\0') { if (src[len] == '\n') { src[len] = '\0'; } len++; } // Copying the source string to destination using my_strcpy my_strcpy(dest, src); // Outputting the copied string (in destination) printf("Copied string: %s\n", dest); return 0; }
Leave a Comment