String Reverse F(x) - Problem w/String Literal Pointers

I'm trying to make a reverse string in place function. I got it to work, but I wanted to get fancy w/pointers and now get "Segmentation Fault (core dumped)".

I don't understand what I'm doing wrong. Help appreciated.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
void swap(char *a, char *b) {
	char temp = *b;
	*b = *a;
	*a = temp;
}

void str_rev(char *s) {
	int n = strlen(s) - 1;

	for(int i = 0; i < (n+1)/2; i++)
		swap(&s[i], &s[n-i]);

}
String literals are read-only, they cannot be "reversed in place" or modified in any other way.

(incidentally, C++ already has swap(), not to mention reverse())
Cubbi already said it. here are the websites, don't forget to include <algorithm>
http://www.cplusplus.com/reference/algorithm/swap/
http://www.cplusplus.com/reference/algorithm/reverse/
Okay, thanks a lot. I was trying to implement one myself for fun and to mess w/pointers.

Thanks again.
Topic archived. No new replies allowed.