In my previous post someone suggest me this. (my question is how to use pointer to print string backwards.
"
Declare your char variable and initialise it to NULL:
char text[ 20 ] = { NULL };
Get you input and then create a loop to see how many characters you have:
1 2 3 4 5 6 7 8 9 10
int szChar = 0;
for( int i = 0; i < 20; i++ )
{
if( text[ i ] == NULL ) //if the index is null, you've hit the end of the input
break; //exit the for loop
szChar++;
}
Now that you have the end of the input, point your pointer to this index:
pIndex += szChar;
Create a loop to loop from this lenght back to 0. Something like:
1 2 3 4 5
for( int i = szChar; i > 0; i-- )
{
std::cout << *pIndex;
pIndex--;
}
And don't forget, when creating pointers to free the memory used afterwards:
1 2
pIndex = NULL;
delete pIndex;
"
1) i don't undersatnd "Now that you have the end of the input, point your pointer to this index:
pIndex += szChar;
" i try to code it as he said but pIndex is undefined.
#include<iostream>
#include<cstring>
usingnamespace std;
int main(void)
{
char text[ 20 ] = { NULL };
int szChar = 0;
cout<<"input a string";
cin>>text;
cout<<text;
for( int i = 0; i < 20; i++ )
{
if( text[ i ] == NULL ) //if the index is null, you've hit the end of the input
break; //exit the for loop
szChar++;
}
index += szChar;
for( int i = szChar; i > 0; i-- )
{
std::cout << *index;
index--;
}
index = NULL;
delete index;
}
i accidently included string because when i started i was using a string, and then realized a char array would be better so i just forgot to delete the include,just an accident. also, whats wrong with system("pause"); system commands arent all that bad!