Here is a simple substring type function that I wrote in C.
char *substr(char arr[], int pos1, int pos2)
{
char str[100];
char *sub;
for (int i = 0; i <= (pos2 - pos1); i++)
{
str[i] = arr[i+pos1];
}
sub = str;
printf("Sub: %s\n", sub); // debugging purposes
return sub;
}
Here is my function call in main.
int main(int argc, char *argv[])
{
char str[] = "Programming in C!";
char *sub = substr(str, 0, 1);
printf("String: %s", sub);
return 0;
}
And here are my commands and output.
% cc -v -o search searchstr2.c
% search
Sub: Prúü
String:
Here's what I want the output to be is:
Sub: Pr
String: Pr
I can't figure this out and it's driving me bonkers. Any help would be greatly appreciated.
Kind Regards,
--
Jay