You are not logged in. Please login at www.codechef.com 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.

THANKS IN ADVANCE

Here's the code if you wish to see here

#include<bits/stdc++.h>
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;

while(i<j)
{
    if(!isalpha(in[i]))
        i++;
    else if(!isalpha(in[j]))
        j--;
    else {
        swap(in[i] , in[j]);
        i++;
        j--;
    }       
}   
//out = in;

return in;

}

asked 24 Apr '17, 02:18

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%


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

Runtime error
Compile Time
Compile Message
solution.cc: In function ‘int main()’:
solution.cc:8:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 char* in = "ab$cd";
            ^~~~~~~
solution.cc:10:13: 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

link

answered 24 Apr '17, 02:42

vijju123's gravatar image

4★vijju123 ♦♦
15.4k12065
accept rate: 18%

edited 24 Apr '17, 02:46

1

@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.

link

answered 24 Apr '17, 02:47

meooow's gravatar image

6★meooow ♦
7.1k718
accept rate: 48%

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

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

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
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×2,714
×1,911
×640
×518

question asked: 24 Apr '17, 02:18

question was seen: 392 times

last updated: 24 Apr '17, 03:18