Solved programming problem

Find Reverse of a String Using Pointer in C.

Source code

#include <stdio.h>

void rev(char *ptr1, char *ptr2)

{

    char *t;

    t = ptr2;

    while(*ptr2 !='') ///count the length

    {

        ptr2 ++;

    }

    ptr2 --;

    while(t != ptr2) ///save reverse

    {

        *ptr1 = *ptr2;

        ptr2 --;

        ptr1 ++;

    }

    *ptr1 = *ptr2;

    *(ptr1 + 1) = ''; ///assign the last index as NULL;

}

int main()

{

    char str[100];

    char *ptr1, *ptr2;

    printf("Please Enter A Massage: ");

    gets(str);

    ptr1 = (char*)malloc(100*sizeof(char)); ///create character type memory

    ptr2 = (char*)malloc(100*sizeof(char));

    ptr2 = str;

    rev(ptr1,ptr2);

    while(*ptr1 != '')

    {

        printf("%c",*ptr1);

        ptr1 ++;

    }

    return 0;

}

Output

Please Enter A Massage: code to haven
nevah ot edoc

Join the discussion

Add your comment