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)!='
#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';
}
') len++; for(i=len-1; i>=0; i--) { *(str2+r) = *(str1+i); r++; } *(str2+r) = '
#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';
}
'; }

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

9 thoughts on “To reverse the given string using pointer

  1. #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();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in