OK, I have a working program which meets the assignment criteria....almost.
Here is the original requirements:
Problem as follows:
Write a function named GetSubString that copies a portion of a string. Syntax is as follows: char * GetSubString(const char source, int start, int count, char result);
Where source is the character string containing the substring, start is the position of the first character in source to be copied, count is the number of char to copy and result is an array into which substring is copied. GetSubString returns a pointer to the copied substring but does no printing itself. For example call printf(GetSubString("character", 4, 3, resultArray)); would copy substring "act" from "character" into resultArray, return resultArray, then print "act".
Following are additional requirments:
1. GetSubString must append a '\0' to every substring it creates
2. If start is beyond end of source string, create a substring containing only a '\0'.
3. If count extends beyond end of source, copy characters only up to the end of source string, even though more have been requested
4. No calling of any library functions
5. You may declare only one variable other than formal parameters, if you do it must be a pointer.
6. You must use the following Main, UNALTERED:
I have a couple problems with my code that I can see:
1. By incrementing variable start on in my while loop, the next time the function is called it does not operate properly, namely my if statement evaluates start which was adjusted in the last function call.
2. I am only allowed to use one variable in the whole program, and that must be a pointer. So I need to work out int a and int i out of my code.
Can anyone show me how to remedy these two issues and get this program running properly? Thanks for all your help. This forum is great. Code is as follows:
#define RESULT_MAX 19
char *GetSubString(const char *source, int start, int count, char *result)
int i = 0;
int a = 0;
char *p = source;
while ( *p )
if( start > i)
result = '\0';
printf("You lose"); // debug check
while ( a < count && ( *result ) && a < RESULT_MAX )
result[a] = source[start];
result[a] = '\0';
static char source = "one two three";
//printf(GetSubString("character", 4, 4, result));
puts(GetSubString("This is really fun", 2, 800, result));
puts(GetSubString("This is really fun", 261, 9, result));
puts(GetSubString("This is really fun", 0, 12, result));
puts(GetSubString("source", 5, 87, result));
puts(GetSubString("source", 18, 7, result));
puts(GetSubString("source", 6, 5, result));
puts(GetSubString("source", 0, 3, result));