I have a reverse function that I would like to use to compare two strings against each other, but it doesn't seem to work. I think that every time the string is iterated over it overwrites the value every time. I'm think that it needs to be stored in a separate array to be able to evaluate the two against each other. Here's what I have so far:
int compare_strings(char [], char []);
char* reverse(char* str)
{
int i, j;
char temp;
i=j=temp=0;
j=strlen(str)-1;
for (i=0; i<j; i++, j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
return str;
}
int main(int argc, char* argv []) {
char word[100];
char const* const fileName = argv[1];
FILE *file = fopen(fileName,"r");
char s[100];
char formattedWord[100];
while(fgets(word, 100, file)!=NULL){
memset(s,'\0',sizeof s);
char *token=strtok(word," ");
while(token!=NULL){
size_t n=strlen(s);
sprintf(&s[n],"%s",token);
token=strtok(NULL," ");
}
if (reverse(s) == s)
{
printf("%s\n", reverse(s));
}
}
fclose(file);
return 0;
}