switch a word

So the thing is you have to put in a name, like "wtr".
Then you have to check what's the length of the word.


Now we have to switch the other word, so new_worth have to become : rtw.

But my problem is after the loop to switch the word its still the same as wtr.

I think it must be made with pointers? But can anyone help me?


Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43




#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;
}
1
2
3
4
5
6
for(size_t i = 0; i < length; ++i)
{
	new_word[i] = word[length - 1 - i];
}

new_word[length] = 0

Are you supposed to use C-style strings? Doing it the C++ way would be much cleaner.
Last edited on
Ye, it's something for my school :/ isnt there a easier way?
cant you do that with pointers because i dont know why you give me this :
1
2
3
4
5
6
for(size_t i = 0; i < length; ++i)
{
	new_word[i] = word[length - 1 - i];
}

new_word[length] = 0


word = wtr
new word = rtw (from right to left)
but that all works but after the for loop for switching my word its the same as before...







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.
wtr wrote:
but that all works but after the for loop for switching my word its the same as before...

The loop I wrote is supposed to replace your for loop. I haven't tested it, but I think it should work properly.
Ye but we have to do it in C so...
Ye but we have to do it in C so...

The solution I provided is pure C. Does it work?
But the problem is i have to save new_word to the switch word iff you understand me? my english isnt so good sorry.

First i need to read in the word like wtr -> WORD : WTR
then i have to switch it -> NEW_WORD : RTW

and then check iff the words are the same in this case it's not...
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.
Last edited on
Topic archived. No new replies allowed.