Thread: String Reversal

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Post String Reversal


    Hello,
    I have written c code for string reversal.
    Program compiles without any error.
    But it gives runtime error.

    I am not able to catch the code in my error, Please help me.

    The code is as follows.

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



    int main()
    {

    int n_loopcount;
    char *source= "Hello";
    char *result= " ";
    int n_count = 0;

    while(*source!= '\0')
    {
    source++;
    n_count++;
    }
    source--;

    for ( n_loopcount = 0; n_loopcount<n_count;n_loopcount++)
    {

    //printf("%c\n",*source);

    *result = *source;
    result++;
    source--;
    }



    printf("%s\n",*result);
    }
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    result only has two bytes - it's not long enough to hold a copy of source.

IMN logo majestic logo threadwatch logo seochat tools logo