Reverse A String

hi i am doing a sample but i think something i wrong this is my code
any idea please

#include<conio.h>
#include<iostream>
using namespace std;
#define lim 5
int main(){

int syc;
char c[lim];
char *cp=c;
cout<<"girin \n\n";
for(syc=0;syc<lim;syc++){
*cp=cin.get();
cp++;
}
cp=c+(lim-1);
for(syc=0;syc<lim;syc++){
cout.put(*cp);
cp--;


}

return 0;
system("pause");
}
I see only two wrong things.
You decrease cp such a way that the final value of cp will point beyond the first element of the array. The C/C++ standards do not allow that a pointer points beyond the first elemnt of an array.

So the second loop I would rewrite the following way


1
2
cp = c + lim;
for ( syc = 0; syc < lim; syc++ ) cout.put( *--cp );


And the system should be before the return

That is instead of
1
2
return 0;
system("pause");


there should be

1
2
system("pause");
return 0;


Also header <cstdlib> is required.
Last edited on
thank you vlad
Topic archived. No new replies allowed.