I am trying to reverse a string using pointers only. I have tried to give a defined memory address or by passing a variables memory location but they also not worked. I am getting "wll" as output in code::blocks. I hope you can help me to figure out the problem.
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n,j;
char a,*str;
cout<<"Enter a string ";
cout<<a<<endl;
gets(str); //taking string
for(n=0;*str!='\0';str++,n++); //Calculating string length
j=n;
for(int i=1;i<n;i++,n--)
{
*(str+i)=(*(str+i))+(*(str+n)); //reversing string in next three lines
*(str+n)=(*(str+i))-(*(str+n));
*(str+i)=(*(str+i))-(*(str+n));
}
for(int i=0;i<j;i++)
cout<<*(str+i);} //showing reversed String
Comments
Post a Comment