Someone understand

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cstring>
using namespace 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;
}

Last edited on
Because you haven't told c++ what 'index' is. And you shouldn't have to as there is no need for it. Where you have
1
2
3
4
5
6
7
8
9
10
index += szChar;
		for( int i = szChar; i > 0; i-- )
	{
		std::cout << *index;
		index--;
	}

index = NULL;
delete index;
}


you should change it to
1
2
3
4
5
6
7
for( int i = szChar; i >= 0; i-- )
	{
		std::cout << text[i];
	}

	return 0;
}


you should notice that 'i > 0' changed to 'i >= 0'
also when you do need to use pointers ALWAYS delete it first THEN set it to NULL;
closed account (D80DSL3A)
Attempting to end the madness...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(void)
{
	char name[20];
	char* pName = name;// if you must use a pointer
	cout << "Enter name: "; cin >> name;
	int endIdx = strlen(name);// find location of null terminator	

	// print name backwards using pointer
	for(int i=endIdx-1; i>=0; --i)
		cout << *(pName + i);

	cout << endl;
	return 0;
}
try this, its far simpler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()
{
	char name[] = "RandName";
	int counter = 0;

	for each (char i in name)
	{
		counter++;
	}

	for (int i = counter - 1; i >= 0; i--)
	{
		cout << name[i];
	}

	cout << endl;
	system("pause");
	return 0;
}

Even easier... and without invalid syntax.

1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
using namespace std;
int main(){
 string me = "emesreveryeh";
 for (int temp = me.size() -1; temp >=0; temp--)
  cout << me[temp];
}
@ascii: Why would you include string, then not use string? Also, system pause
closed account (D80DSL3A)
How about a version using a reverse iterator for the string class? Seems like a good place to use one:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
using namespace std;
int main(void){
	string name("eM esreveR");	
	for(string::reverse_iterator rit = name.rbegin(); rit != name.rend(); ++rit)
		cout << *rit;
}

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!
I was drunk when I wrote that code! lol. Sorry.

This:
Now that you have the end of the input, point your pointer to this index:

pIndex += szChar;


I made a pointer called pIndex, like you made a pointer called "name".
pIndex = &text[ 0 ];

And then added the size of the array to the pointer.
pIndex += szChar;

If I had done the following:
pIndex += 1; would therefore be pointing at text[ 1 ];

So by doing pIndex += szChar; I'm just jumping to the end of the array.
Last edited on
Topic archived. No new replies allowed.