#include <stdio.h>
#include <string.h>

int main(void){
	//open input and output files
	FILE * in = fopen("in.txt","r");
	FILE * out = fopen("out.txt","w");

	//declare character array to hold story
	char story[5000];

	//use fgets to read story from file into story variable
	fgets(story,5000,in);

	char * startOfSearch = story;

	char * cp;

	//while there are still ( in the file
	while(strchr(startOfSearch,'(')!=NULL){
		//find first parentheses set
		char * op = strchr(startOfSearch,'(');
		cp = strchr(startOfSearch,')');

		//copy what was between those two parentheses into a new string
		//so we can prompt the user
		char prompt[100];

                int i = 0;
                //initialize the response array to null terminators
                for (i = 0; i < 100; i++){ 
                        prompt[i] = '\0';
                }

		strncpy(prompt,op+1, cp-op-1);
		
		prompt[cp-op] = '\0';

		//ask the user to enter something for the prompt
		printf("Please enter a %s:",prompt);

		//read it in
		char response[100];

		scanf("%s",response);

		//print the story up until the prompt to the file
		char storypart[1000];
		strncpy(storypart,startOfSearch,op-startOfSearch);
		storypart[op-startOfSearch] = '\0';

		fprintf(out,"%s%s",storypart,response);	

		//move to the next prompt by moving the start of story
		startOfSearch = cp + 1;
	}

	char endofstory[1000];
	strncpy(endofstory,cp,(story+1000)-cp);
	fprintf(out,"%s",endofstory);

	//close files
	fclose(in);
	fclose(out);

	return 0;
}
