You are not logged in. Please login at to post your questions!


Why is this program not working as expected ?

I am trying to reverse a string without affecting the position of special characters in it. But, my program is not running properly and it gives a runtime error when executed on Ideone - link.

From what I can think is that there is some problem with the char* variable that I am using there. Please correct me and provide an explanation why this is happening.


Here's the code if you wish to see here

using namespace std;

char *func(char*);

int main(){

char* in = "ab$cd";

char* out = "";

out = func(in);

printf("%s" , out);

return 0;


char* func(char* in){

//char *out = "";
int len = strlen(in);
int i = 0;
int j = len - 1;

    else if(!isalpha(in[j]))
    else {
        swap(in[i] , in[j]);
//out = in;

return in;


asked 24 Apr '17, 02:18

shubham99's gravatar image

accept rate: 5%

I got this list of errors on running this at hackerrank compiler-

Runtime error
Compile Time
Compile Message In function ‘int main()’: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 char* in = "ab$cd";
            ^~~~~~~ warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

I think you should google this thing up. Sites like Stackoverflow should have something related to it.

EDIT- Link


answered 24 Apr '17, 02:42

vijju123's gravatar image

4★vijju123 ♦♦
accept rate: 18%

edited 24 Apr '17, 02:46


@vijju123 Thanks for answering. Actually I did google it but couldn't find anything worth and I find codechef community better than SO community that's why I always come here first.

(24 Apr '17, 03:06) shubham992★

That's true dear. Also, I don't expect google to yield results without knowing the specific error. Once we get hold of the name, then things get easy :)

(24 Apr '17, 03:18) vijju123 ♦♦4★

I think this link explains the issue satisfactorily.
Assigning a string to "char *in" prevents you from modifying it in the future. Use "char in[]" instead.


answered 24 Apr '17, 02:47

meooow's gravatar image

6★meooow ♦
accept rate: 48%

Yes, that's what my link says too. Thanks @meooow

(24 Apr '17, 02:49) vijju123 ♦♦4★

Yes, actually we posted our links almost simultaneously, so I only saw yours after the page refreshed :)

(24 Apr '17, 02:54) meooow ♦6★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 24 Apr '17, 02:18

question was seen: 392 times

last updated: 24 Apr '17, 03:18