To reverse the given string using pointer

This is the program to reverse the given string and display. The program internally uses the logic of reversing the word.

Logic: The approach here is to reverse the string using the pointers. Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string pointer arguments, the source and destination. It has an iterative loop, which traces from the EOL through the beginning. Each time it copies the current letter to the destination. Finally it displays the resultant string.

The earlier program implements the same by direct method, i.e. without pointers.

#include<stdio.h>
#include<conio.h>
void strev(char *str1, char *str2);
void main()
{
        char *str1, *str2;
        clrscr();
        printf("\n\n\t ENTER A STRING...: ");
        gets(str1);
        strev(str1,str2);
        printf("\n\t THE REVERSED STRING IS...: ");
        puts(str2);
        getch();
}

void strev(char *str1, char *str2)
{
        int i = 0, len = 0, r = 0;
        while(*(str1+len)!='\0')
                len++;
        for(i=len-1; i>=0; i--)
        {
                *(str2+r) = *(str1+i);
                r++;
        }
        *(str2+r) = '\0';
}

9 Responses to “To reverse the given string using pointer”

  1. i need codes for yours projects
    so please send me full details
    don’t forget me
    thank uuuuuuuuuuuuuuuuuuuuu

    Reply
  2. char *s1;
    gets(s1);

    this is not correct as s1 is not initialized. its giving segmentation fault.

    Reply
  3. Gaurav Sharma

    void main(void)
    {
    uchar *str “ABCDE”;
    uchar len;
    uchar newidex = 0;

    len=0;
    while( *str != ” ) len++;

    str = revstr(str, len, newidex);
    PRINT(str);
    }

    uint* revstr(uchar *str, uchar len, uchar &index)
    {
    uchar *b;

    if(len==0)
    {
    return b;
    }
    else
    {
    b[index++] = str[len];
    revstr( str, len-1, index );
    }
    }

    Reply
  4. Gaurav Sharma

    correction in above code is:
    while( *(str+len) != ” ) len++;

    Reply
  5. #include
    #include
    #include
    void main()
    {
    char *str,temp;
    int i,j;
    printf(“\n Enter the string: “);
    scanf(“%s”,str);
    for(i=0,j=strlen(str)-1;i<=j;i++,j–){
    temp=*(str+i);
    *(str+i)=*(str+j);
    *(str+j)=temp;
    }
    printf("%s",str);
    getch();
    }

    Reply
  6. kulbhushan

    IN the above program one thing i think is missing again come back to sting. if ” THis is a string”. in the above program it will be only “siht” and the loop will not come back. if we do one more codition like.
    while (*(str1) != NULL)
    {
    while (*(str1+len) != ‘ ‘)
    len++
    for(i=len-1; i>=0; i–)
    {
    *(str2+r) = *(str1+i);
    r++;
    }
    *(str2+r) = ”;
    }

    Reply
  7. siddharth rawat

    #include
    #include
    void main()
    {
    int i=0;
    while(*(a+i)!=NULL)
    {i++;}
    printf(“\n\n”);
    for(i;i>=0;i–)
    printf(“%c”,*(a+i);
    getch();
    }

    Reply

Leave a Reply