Stack Overflow using Shortest code to reverse string

Shortest code to reverse string not running

Page 1 of 1

2 Replies - 3670 Views - Last Post: 03 January 2011 - 09:03 AM Rate Topic: -----

#1 srijani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 09

Stack Overflow using Shortest code to reverse string

Posted 03 January 2011 - 05:24 AM

Friendz i previously tried using the shortest code to reverse a string using c++ in the c-free ide and t worked fine but now i am trying it in gcc and i get a segmentation fault error. in windows, running the same in VS2010 gives a stack overflow error..

#include<stdio.h>

void revstr(char *s){
if(*s){
revstr(s+1);
putchar(*s);
}
}
int main(){
char *s;
gets(s);
revstr(s);
return 0;
}



$ ./a.out
srij
Segmentation fault

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 January 2011 - 05:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stack Overflow using Shortest code to reverse string

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1768
  • View blog
  • Posts: 3,428
  • Joined: 30-May 10

Re: Stack Overflow using Shortest code to reverse string

Posted 03 January 2011 - 05:39 AM

> char *s;
> gets(s);
1. Your pointer doesn't point anywhere (it is uninitialised).
2. You used gets() - NEVER use gets() -> http://sourceforge.n....php?title=Gets
Was This Post Helpful? 1
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Stack Overflow using Shortest code to reverse string

Posted 03 January 2011 - 09:03 AM

There is a short way to do it (O(n/2)) that I posted not too long ago. I don't think using standard functions count.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1