I have to try and use the GetSubstring function to print the result so that
printf(GetSubstring("character", 4, 3, resultArray));
outputs act
Limitations: can’t call other functions or macros, can’t add any other variables, can’t set variable to 0. Can only change function GetSubstring.
Here is my current code.
#include <stdio.h>
#include <stdlib.h>
char *GetSubstring(const char source[], int start, int count, char result[]);
int main(void)
{
const char source[] = "one two three";
char result[] = "123456789012345678";
puts(GetSubstring("character", 4, 3, result));
return(EXIT_SUCCESS);
}
char *GetSubstring(const char source[], int start, int count, char result[])
{
char *copy = result;
for (; *source != '\0' || *source == source[start]; start--)
{
while (*source != '\0')
{
*result++ = *source++;
}
}
*result = '\0';
return (copy); // outputs character
// return (result); // outputs 012345678
}
Thank you for your help.
This line is supposed to terminate the string:
But it doesn’t because you are adding zero to the existing value. Try setting it to zero instead:
More importantly, your loop is wack. This contains a whole lot of recipes for trouble:
Why don’t you begin at
start, and then increment…