85.9k views
0 votes
Exclude any existing source code files that may already be in your IDE project and add two new ones, naming them C1A6E3_GetSubstring.c and C1A6E3_main.c. Do not use #include to include either of these

User Dan Scally
by
8.5k points

1 Answer

2 votes

Answer:

#include <stdio.h>

#include <string.h>

void GetSubstring(const char* str, int start, int length, char* result)

{

int strLength = strlen(str);

if (start < 0 || start >= strLength || length <= 0 || start + length > strLength)

{

result[0] = '\0'; // Empty string

return;

}

strncpy(result, str + start, length);

result[length] = '\0';

}

#include <stdio.h>

extern void GetSubstring(const char* str, int start, int length, char* result);

int main()

{

const char* str = "Hello, World!";

int start = 7;

int length = 5;

char result[length + 1]; // Add 1 for null terminator

GetSubstring(str, start, length, result);

printf("Substring: %s\\", result);

return 0;

}

User RogerDarwin
by
7.5k points