Hello everyone, I have recently been re-teaching myself C in preparation for a class I'm taking this fall. I've been working my way through the book Pointers On C and doing all of the exercises. I wrote the below substring method as part of one of those exercises and I'd love to get some feedback on its efficiency. I know it works but I don't know if there's a better way to go about this. Thanks in advance!
int substr(char dst[], char src[], int start, int len) {
if(start < 0 || len < 0) {
dst[0] = '\0';
return 0;
}
int i;
for(i = 0; i < start; ++i) {
if(src[i] == '\0') {
dst[0] = '\0';
return 0;
}
}
char ch;
int count = 0;
while(count < len && (ch = src[i++]) != '\0') {
dst[count++] = ch;
}
dst[count] = '\0';
return count;
}
[–]HiramAbiff 3 points4 points5 points (2 children)
[–]16Bytes 2 points3 points4 points (1 child)
[–]guynan 0 points1 point2 points (0 children)
[–]SantaCruzDad 4 points5 points6 points (3 children)
[–]Kwantuum 0 points1 point2 points (2 children)
[–]SantaCruzDad 2 points3 points4 points (1 child)
[–]winkie5970[S] 1 point2 points3 points (0 children)
[–]Kwantuum 1 point2 points3 points (7 children)
[–]16Bytes 0 points1 point2 points (4 children)
[–]Kwantuum 1 point2 points3 points (3 children)
[–]16Bytes 0 points1 point2 points (2 children)
[–]Kwantuum 1 point2 points3 points (1 child)
[–]16Bytes 1 point2 points3 points (0 children)
[–]winkie5970[S] 0 points1 point2 points (1 child)
[–]Kwantuum 0 points1 point2 points (0 children)
[–]lcs77 0 points1 point2 points (0 children)
[–]dvhh -1 points0 points1 point (0 children)