You may want to try looking at this:
C String Functions
I cooked this up real quick, I'm not totally sure if this is what you wanted though:
Code:
#include <stdio.h>
#include <stdlib.h>
char *getSubstring(char *haystack, int start, int end);
int main(void) {
char *myString = "Hello World";
char *mySubString = NULL;
if( (mySubString = getSubstring(myString, 1, 4)) == NULL) {
puts("Substring error");
return 0;
}
puts(mySubString);
return 0;
}
char *getSubstring(char *haystack, int start, int end) {
char *substring = malloc(255 * sizeof(*substring));
if(end < start)
return NULL;
int i, location=0;
for(i=start; i<=end; ++i) {
if(haystack[i] == '\0') return NULL;
substring[location++] = haystack[i];
}
substring[location] = '\0';
return substring;
}
The output is:
Code:
kleid@Shiva:~/Programming/Laboratory$ ./a.out
ello