1

I am trying to reverse a string using command line argument but I don't know why am I getting segmentation fault for this .

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

int main(int argc, char *argv[]) {
    char *str = argv[1];
    char *rev;

    int i, j, k;

    for (i = 0; str[i] != '\0'; i++); {
        k = i - 1;
    }
    for (j = 0; j <= i - 1; j++) {
        rev[j] = str[k]; 
        k--;
    }
    printf("The reverse string is %s\n", rev);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • What's the point of `len`? – Eugene Sh. Oct 12 '17 at 17:06
  • 5
    you never initialize `char *rev;` to point at a valid memory location – UnholySheep Oct 12 '17 at 17:07
  • You aren't allocating any storage for `rev`, and even if you were, you aren't copying the terminating `'\0'`. Also, why did you place the initial assignment to `k` in a block? It's very misleading, given the preceding one-line `for` loop. And why are you using the loop? You already have `len`, so `k` is just `len - 1`. – Tom Karzes Oct 12 '17 at 17:08
  • Because `rev` doesn't point anywhere meaningful, it's just an uninitialized pointer. Use `malloc()` or a variable-length array --- or a fixed-size array if an upper bound for string length is ok. –  Oct 12 '17 at 17:08
  • You need to allocate `len + 1` memory to `rev` variable. – H.S. Oct 12 '17 at 17:08
  • Aside about user input: assume nothing / check everything. In this case you have accessed `argv[1]` (through `str`) without checking `argc >= 2`. – Weather Vane Oct 12 '17 at 17:15
  • 1
    Well, now that it's been reformatted, it's clear that you believe the first assignment to `k` is inside the preceding `for` loop. It isn't, nor does it need to be. The semicolon terminates the `for` loop, and the block that follows it outside of the loop. – Tom Karzes Oct 12 '17 at 17:24

4 Answers4

1

You have to allocate memory for reverse string and make rev to point to that location.

 char *rev = malloc(strlen(argv[1]) + 1); // 1 extra space to store null character.

And terminate the reversed string at the end

rev[j] = '\0';
haccks
  • 104,019
  • 25
  • 176
  • 264
0

After the for loop

    for(j = 0; j <= i-1; j++)
    {
        rev[j] = str[k];
        k--;
    }
    //Add this line
    rev[j]='\0'; //always terminate character array with null.
    printf("The reverse string is %s\n",rev);
0

There are multiple problems in the code:

  • There is an extra ; in the line for(i = 0; str[i] != '\0'; i++); {
  • you store characters to rev but this pointer is uninitialized, the behavior is undefined.

You can just reverse the string in place this way:

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

int main(int argc, char *argv[]) {
    if (argc > 1) {
       char *str = argv[1];
       int i, j, len = strien(str);

       for (i = 0, j = len - 1; i < j; i++, j--) {
           char c = str[i];
           str[i] = str[j];
           str[j] = c;
       }
       printf("The reverse string is %s\n", str);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
-1

You can use recursion. For example

#include <stdio.h>

int main( int argc, char * argv[] ) 
{
    if ( !( argc < 2 ) )
    {
        if ( !*argv[1] )
        {
            printf("The reverse string is " ); 
        }
        else
        {
            ++argv[1];
            main( argc, argv );
            --argv[1];
            putchar( *argv[1] );
        }
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335