The .txt file says "The Homework is due on Friday. Good$?Luck!"
Console:
The Homework 1 is due on Friday. Good$?Luck!
ehT krowemoH 1 si eud no .yadirF !kcuL?$dooG
It should be like this: ehT krowemoH si eud no yadirF. dooG$?kcuL!
I'm trying to get the program to ignore special characters, but am struggling as to where to add an if statement.
Code:
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void reverse_string(char*);
void reverse_words(char*);
int main()
{
FILE *fp;
char Input[255];
fp = fopen("test.txt", "r");
fgets(Input, 255, (FILE*)fp);
printf("%s\n", Input);
reverse_words(Input);
printf("%s", Input);
return 0;
}
void reverse_words(char *s) {
char b[100], *t, *z;
int c = 0;
t = s;
while(*t) { //processing complete string
while(*t != ' ' && *t != '\0') { //extracting word from string
b[c] = *t;
t++;
c++;
}
b[c] = '\0';
c = 0;
reverse_string(b); // reverse the extracted word
z = b;
while (*z) { //copying the reversed word into original string
*s = *z;
z++;
s++;
}
if (*s == ' ') //Skipping spaces
{
s++;
}
t = s; // pointing to next word
}
}
void reverse_string(char *t) {
int l, c;
char *e, s;
l = strlen(t);
e = t + l - 1;
for (c = 0; c < l/2; c++) {
s = *t;
*t = *e;
*e = s;
t++;
e--;
}
}