#include "stdafx.h"
#include <string.h>
#define AANTAL 8
int _tmain(int argc, _TCHAR* argv[])
{
int i;
int positie = 0;
char word[AANTAL];
char new_word[AANTAL];
int ptr;
printf("Put a word here: ");
scanf("%s",&word);
int length = strlen(word);
strcpy(new_word,word);
printf("%s",new_word);
for(i=length; i>= 0;i--){
scanf("%c",&new_word[i]);
}
//The problem must be here, new_word is still the same as before...
printf("%s",new_word);
//here i wanted to check iff the words are the same or not...
ptr = strcmp(new_word,word);
if(ptr == 0){
printf("They are the same");
}else{
printf("They are not the same");
}
scanf(" ");
return 0;
}
As far as I know (and could check), the cstring standard header doesn't offer a direct way to do this. I'm guessing yours is a C class because the whole program you wrote is in C style. C++ would allow you to use std::string and std::reverse_copy(), which would solve this in a few lines as well.
That snippet is meant to do just that. It assumes word has been read, new_word is an array of the same size, and length contains the string length. It iterates over each of new_word's elements, assigning the "opposite" element in word to them. Finally, it sets the element past the string in new_word to the null character, to make it a proper C string. After it runs, word is untouched and new_word should contain the reversed string.