Archived
1
0
Fork 0
This repository has been archived on 2023-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
SLOTH/src/string.c
Brandon Rozek 53575d25f9 Converted NULL to nullptr
Converted fprintf to cout/cerr
Removed as much C dependencies as possible
2018-09-26 20:03:54 -04:00

28 lines
692 B
C

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
char* substring(char* str, int start, int end) {
// Add conditions that substring is valid, probably using strlen
int stringLength = strlen(str);
if (start >= stringLength || end > stringLength) { fprintf(stderr, "Requesting substring outside of valid range.\n"); return NULL; }
char* substr;
int c;
int length = end - start;
substr = (char*) malloc(length + 1);
if (substr == nullptr) {
fprintf(stderr, "Unable to allocate memory for substring.\n");
exit(1);
}
for (c = 0; c < length; c++) {
substr[c] = str[start + c];
}
substr[c] = '\0';
return substr;
}