Reversing a char*

I'm posting a snippet of my project that I'm currently having trouble with.

In the function reverse_ I'm passing "dog", just to simply see if "dog" would reverse to "god". However I keep getting an exception thrown error at *s=*end. I'm not quite sure what this exception thrown error is telling me exactly other than it has to do something with memory or how to fix it.

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
size_t strlen(const char* p)
{
	int len;
	len = 0;
	while (*p != '\0')
	{
		p++;
		len++;
	}
	return len;
}
  char* reverse_(char *s){
	
	char* end = s + strlen(s) - 1;      //so that char* end starts on "g" 
                                              //rather than the null terminator

	while (s < end) {
		char c;
		c = *s;
		*s = *end;                         //here i get the following: Exception thrown: write 
                                                    // access violation.   s was 0x2B8B30.
                     
		*end = c;
		s++;
		--end;
		
	
	}
	return s;
}
Last edited on
How did you declare the original string "dog"?
Possibly you declared it as read-only?

Try this:
1
2
    char word[] = "dog";
    reverse_(word);


Also which character is s pointing to when return s; is executed at line 29?
Last edited on
ah you're right, I declared "dog" as a read-only, explains why it wasn't working earlier. Now reverse_ almost works, excepts it only outputs "od" instead of "god"


To be completely honest, I'm not sure what the character s is pointing to when returning at line 29.

(edit): here is my int main if it helps as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#define BUF_SIZE = 100;
int main(int argc, const char * argv[])
{

	
	char aname[] = "dog";

	
	std::cout << "this reversed: " << reverse_(aname);
	

	return 0;
}
Last edited on
To be completely honest, I'm not sure what the character s is pointing to when returning at line 29.

Well, providing the rest of the reverse function is working (it seems to be), s is pointing to somewhere half-way through the sting. If it has an odd number of letters ("dog" has 3) then it points to the middle letter 'o'. If it is even ("abcd" has 4) then it points to the letter just past the middle.
Last edited on
if i change dogs to abcd, i get an output of "ba". I'm still confused about what to do about return s and how to output it so that I get "dcba"
When I first ran the program, I didn't notice the problem with the return value. I just did this in main()

1
2
3
4
	char word[] = "abcd";
	std::cout << "word = " << word << '\n';
	reverse_(word);
	std::cout << "word = " << word << '\n';

But to fix the problem, the simplest approach is to save the original value of s. i.e. use a separate variable to store the value of s at the start of the function. Then at the end, return that original value.

(Or something like that. There's more than one way to write the code).
Before i saw your reply, I did this and now my program works fine, would this be okay?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* reverse_(char *s) {
	char *temp = s;
	char* end = s + strlen(s) - 1;

	while (s < end) {
		char c;
		c = *s;
		*s = *end;
		*end = c;
		s++;
		end--;
		


	}
	return temp;
}

the program asks me to reverse a string in place so i'm not sure if this is within the guidelines of what the professor wants
Last edited on
"Reverse a string in place" - yes, I would think that your code meets that requirement. That is, it doesn't make a copy, it actually modifies the original string - which is why it gave a problem when the string was read-only. So yes, it's ok.

One suggestion. Instead of calling a separate strlen() function, you might integrate that into the reverse function.

All you need do is set end to the same as s. Then loop until the null is found. Then subtract 1, so end points to the last actual character.
thank you so much! :)
Topic archived. No new replies allowed.