-1

I am trying to reverse this string using the pointers and char but the output I am getting is missing the 2nd character and couldn't find what is the reason. like in below case I am missing the word B.

#include <iostream>
#include <cstring>
#include <string>
using namespace std;


int main()
{

        int size = 100;
        char oldText[size];
        char newText[size];

        char *pntr;
        pntr = oldText;

        cout << "Write the Text: \n";
        for (int i=0; i<size; i++)
        {


            cin.get(pntr,size);
            newText[i]=*pntr;
            cout << *pntr;
            pntr++;
        }

        cout << "The text backwards is: \n";
        for (int i = size; i>=0; i--)
        {
            pntr--;
            cout <<newText[i];


        }
        cout <<endl;


    return 0;
}

Result for your reference

Umerraja
  • 1
  • 1
  • 2
    please do not use images in questions. they are useless. instead try to paste the context of the image in the text. –  Feb 14 '19 at 12:08
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen Feb 14 '19 at 12:40
  • `istream::get (char* s, streamsize n)` extracts `n` characters into `s`, or until it encounters a delimiter. So it will extract upto 100 characters in one call, why are you calling it in a loop? – Unimportant Feb 14 '19 at 12:55

1 Answers1

0

This code here is not valid C++.

int size = 100;
char oldText[size];
char newText[size];

You are using VLAs which are not valid C++ code. Read this answer for more information https://stackoverflow.com/a/54674671/7185790.

Instead you can qualify int size with a constexpr and it'll be ok.

constexpr int size = 10;

See the following example that doens't use a char pointer: https://rextester.com/AEULY24804

See the following example that uses a char pointer: https://rextester.com/FNPW17676