How to implement the strcpy function in C

1 Answer

0 votes
#include <stdio.h>

char* strcpy(char* s1, const char* s2) {
	// copy s2 to s1
	char* p = s1;

	for (p = s1; (*p++ = *s2++) != '\0'; ) 
		;
	
	return s1;
}

int main() {
	char str1[32] = "";
	char str2[32] = "c programming";

	strcpy(str1, str2);

	puts(str1);

    return 0;
}




/*
run:
    
c programming
    
*/

 



answered Dec 18, 2022 by avibootz

Related questions

2 answers 266 views
1 answer 122 views
122 views asked Dec 7, 2022 by avibootz
1 answer 156 views
2 answers 193 views
1 answer 216 views
216 views asked Dec 30, 2021 by avibootz
...