0

I am trying to reverse a string in c but i get segmentation fault, core dumped error, couldn figure out the error.

#include<stdio.h>
#include<string.h>

void main(){
    char inputstr[100];
    int i = 0;
    char revstr[100];

    printf("Give me something to reverse!!:\t");
    gets(inputstr);
    printf("You entered %s\n", inputstr);

    int lenstr = strlen(inputstr) - 1;


    while(lenstr >= i){
        strcpy(revstr, inputstr[lenstr]);
        lenstr = lenstr -1; 
    }

    printf("%s\n", revstr);
}
schil227
  • 292
  • 3
  • 11
vijay shanker
  • 2,517
  • 1
  • 24
  • 37
  • 5
    (1) [Never use `gets`](https://stackoverflow.com/questions/1694036/). (2) `strcpy` does not do what you think it does. (3) Read "[How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)." – zwol Nov 23 '16 at 16:20
  • `for (i = 0; i <= lenstr; i++) { revstr[i] = inputstr[lenstr - i]; } revstr[i] = 0;` would work better. Otherwise, what are you doing with `i`? –  Nov 23 '16 at 16:24
  • 1
    The compiler should be complaining about your call to `strcpy()`. If it isn't, you need to get a better compiler. If it is, you need to heed it; C compilers don't spend their time warning about 'might be a problem'; they warn only about things that are clearly problems. At this stage in your career, the compiler is ominscient; it is right, unconditionally; if it warns, you've got a bug in your code. Ignoring compiler warnings means your program is likely to crash. (It may crash even if the compiler gives no warnings; but if the compiler warns, the crash is much more likely.) – Jonathan Leffler Nov 23 '16 at 16:47
  • 1
    @JonathanLeffler Might be good to mention that `gcc -Wall` is a much better compiler than `gcc` in that sense. – zwol Nov 23 '16 at 16:59

2 Answers2

1

I am trying to reverse a string in c but i get segmentation

strcpy() is used to copy a string into destination string. So the arguments are char* and const char*. But here in your code you are sending a char as the second argument of strcpy() function.

You need not use strcpy() function at all for reversing the string, just a for loop is enough to achieve reversing the string.

Example for for loop:

for (i = 0; i <= lenstr; i++) 
{ 
    revstr[i] = inputstr[lenstr - i]; 
} 
revstr[i] = '\0';
Cherubim
  • 5,287
  • 3
  • 20
  • 37
-1

what should do strcpy()?

Here my version of reverse:

void reverse(char * s, int len)
{
    char c,*end =s+len-1;
    while (s<end)
    {
       c=*s;
       *s++=*end;
       *end--=c;
     }
}

With XOR you can avoid to declare the temporary c variable... but it is not redeable, and I am quite curious to know if gives some performances improvments... I choosed to pass the len, rather than compute it inside, since you can reverse also substrings, or because the length is already availabe

jurhas
  • 613
  • 1
  • 4
  • 12
  • 1
    Downvoted just for *mentioning* the XOR trick, which gives the wrong answer in some circumstances (in particular, in this case it will mangle any string containing an odd number of characters), and on modern CPUs is *slower* than a temp variable. – zwol Nov 23 '16 at 17:02