How to copy one string to another using recursion in C

1 Answer

0 votes
#include <stdio.h>

void copy_recursion(char s1[], char s2[], int index) {
    s2[index] = s1[index];

    if (s1[index] == '\0')
        return;

    copy_recursion(s1, s2, index + 1);
}

int main(void) {
     char s1[16] = "c programming", s2[16];
 
    copy_recursion(s1, s2, 0);

    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    return 0;
}




/*
run:

s1: c programming
s2: c programming

*/

 



answered Jan 16, 2021 by avibootz

Related questions

1 answer 161 views
3 answers 323 views
1 answer 162 views
162 views asked Apr 28, 2018 by avibootz
2 answers 237 views
237 views asked Apr 28, 2018 by avibootz
1 answer 156 views
...