fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *cutstr(const char *srcstr, size_t newlen) {
  6. size_t srclen = strlen(srcstr);
  7. if(newlen > srclen) return NULL;
  8. char *newstr = malloc(newlen + 1);
  9. if(!newstr) return NULL;
  10. strncpy(newstr,srcstr,newlen);
  11. newstr[newlen] = '\0';
  12. return newstr;
  13. }
  14.  
  15. int main(void) {
  16. char *przyciety = cutstr("przytniemy",3);
  17. printf("%s %zu\n",przyciety,strlen(przyciety));
  18. free(przyciety);
  19. return 0;
  20. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
prz 3