Evening,
I am a student learning C and would like some opinions on the follow section of my program. The below section uses STRTOK to read in a string, break it into tokens and finally print to stdout and to file the tokens in the reverse order in which they were read. For the sake of space, I have included only the section that pertains to the STRTOK function. Though the program works, the method in which I reverse the text has me wondering if there is a more effecient way. Basically, I write the contents of each token to a pointer location, and then printf the newly created pointer.
Is there a better way to do this?
Code:
//reverse string section
printf("\n\nThe sentence printed in reverse order is: ");
tkptr = strtok(originalString, seps);
value[r]=tkptr;
while(tkptr != NULL)
{
tkarray[r++] = originalString;
tkptr = strtok(NULL,seps);
value[r]=tkptr;
}
fo=fopen("proj_out.txt","w"); //Opens file for writing.
//iterates through tokens displaying the sentence in reverse and writes out tokens to file.
while (r>0)
{
printf("%s ",value[r-1]);
fprintf(fo,value[r-1]);
fprintf(fo," ");
r--;
}
fclose(fo); // Closes output file pointer
}
Thanks to all who comment,