I am currently writing an assignment that takes a certain text and scrambles the middle letters of every word, but leaves the first and last character of the word alone. The code I have come up with so far works well for some words, but not others, particularly 4 letter words or words with punctuation. Any help on what to do to fix my code would be greatly appreciated! Help.txt
The text the code is supposed to scramble: A6P1_2016_TestingSherlock.txt
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#define INPUT_FILENAME ("A6P1_2016_TestingSherlock.txt")
#define OUTPUT_FILENAME ("A6P1_2016_SherlockEncoded.txt")
void process_file(FILE* ifp, FILE* ofp) {
printf("Begin file processing\n");
char text[10000];
while((fscanf(ifp, "%s", text)!= -1)){
int i;
char c;
int length = strlen(text);
c = text[1];
if(length>3){
for (i=1; i<10000; i++){
if(i==length-2){
text[i]= c ;
}
else if(i==length-1){}
else{
text[i]=text[i+1];
}
}
}
printf("%s ", text);
fprintf(ofp,"%s ",text);
}
printf("\n");
printf("End file processing\n");
} /* process_file */
int main(void) {
printf("Starting text encoding...\n\n");
unsigned int seed = (unsigned int)time(NULL);
srand(seed);
FILE* ifp = fopen(INPUT_FILENAME, "r");
if (ifp == NULL) {
printf("Cannot open input file %s\n", INPUT_FILENAME);
exit(EXIT_FAILURE);
} /* if */
FILE* ofp = fopen(OUTPUT_FILENAME, "w");
if (ofp == NULL) {
printf("Cannot create output file %s\n", OUTPUT_FILENAME);
exit(EXIT_FAILURE);
} /* if */
process_file(ifp, ofp);
fclose(ofp);
fclose(ifp);
printf("\nEncoding finished.\n");
return EXIT_SUCCESS;
} /*main*/