Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading