0

problem is in 15 and 16th line i am unable to store char into a pointer pointing to a string. the problem is same as given in my book.?do i need to change my compiler dev c++? plz help.

#include<iostream>
#include<conio.h>
using namespace std;
void reverse(char *str)
{
    char *end=str;
    char *beg=str;
    char temp;
    while(*end)
    {
        end++;
    }
    end--;
    while(beg<end)
    {
        cout<<*beg<<" , "<<*end<<endl;
        temp=*beg;
        *beg=*end;
        *end=temp;
        beg++;
        end--;
    }
    cout<<str;
}

int main()
{
    char *str="saurabh";
    reverse(str);
    getch();
    return 0;
}
brokenfoot
  • 11,083
  • 10
  • 59
  • 80

2 Answers2

1
char *str="saurabh";

You cannot manipulate "saurabh" because it is literal.

For this you should either copy it to char[],

Example,

char arr[20];
char *ptr = "Data";

strcpy(arr,ptr);
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
0

I know that you likely need to implement yourself reverse, but you can use std implementation for tests:

std::string s("abc");
std::reverse(s.begin(), s.end());
Spock77
  • 3,256
  • 2
  • 30
  • 39