How do i read specific from one specific index to another?

I need to write some code that would allow me to read a string that was input by a user using fgets() but the first character is info about what the program should do.

For example in the program that i’m writing, if input[0] == 'p' i want it to add to a struct the characters that follow it in input [2] through input[4] but every way i’ve tried to do it has failed. Anyone know how i can separate parts of a string?

>Solution :

int startIndex = 2;
int lastIndex = 4;
char answer[lastIndex - startIndex];

for (int i = startIndex; i <= lastIndex; i++) {
    answer[i-startIndex] = input[i];
}

This grabs every character within the start and last index

Leave a Reply