Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

I want to reverse a string using pointers. Can anyone help me to correct this code

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