How to copy part of a string to another string in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[32] = "c programming";
    char dest[32] = "";
    int n = 5;
     
    strncat(dest, s, n);
 
    puts(dest);
}



/*
run:

c pro

*/

 



answered Jun 5, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[32] = "c programming";
    char dest[32] = "";
    int n = 5;
     
    sprintf(dest, "%.*s", n, s);
 
    puts(dest);
}




/*
run:

c pro

*/

 



answered Jun 5, 2022 by avibootz

Related questions

1 answer 183 views
1 answer 194 views
2 answers 282 views
1 answer 164 views
1 answer 160 views
...