How to copy string without using strcpy() in C

2 Answers

0 votes
#include <stdio.h> 

int main(void)
{   
    char s1[20] = "c c++ java", s2[20];
    int i = 0;

    while (s1[i])
    {
        s2[i] = s1[i];
        i++;
    }

    s2[i] = '\0';
    printf("%s", s2);
            
    return 0;
}

  
/*
run:

c c++ java

*/

 



answered Jun 2, 2017 by avibootz
0 votes
#include <stdio.h> 

int main(void)
{   
    char s1[20] = "c c++ java", s2[20];
    int i = 0;

    do 
    {
        s2[i] = s1[i];
    } while (s1[i++]);

    s2[i] = '\0';
    printf("%s", s2);
            
    return 0;
}

  
/*
run:

c c++ java

*/

 



answered Jun 2, 2017 by avibootz

Related questions

2 answers 267 views
1 answer 137 views
137 views asked Dec 18, 2022 by avibootz
1 answer 123 views
123 views asked Dec 7, 2022 by avibootz
1 answer 156 views
1 answer 115 views
...