priting my name backward

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
#include<iostream>
#include<cstring>
using namespace std;



int main(void) 
{
	char temp[20];
	char *name = &temp;
	cout<<"input something";
	cin>>temp;
	//	char *name = temp;



	short len = strlen (name) + 1, x = -1;  //determine the size of "name"
    short size = len;
	char* backward = new char[len];
	do backward [x++] = name [--len];  //copy letter by leter from back to ahead  
    while (len != -1);  // do it until end of name
     


}


There's a logic error and i think that is because since it's a pointer my "backward" is making NULL in front. Anyone know a way to fix that?
this char *name = &temp; is incorrect, either use char *name = &temp[0]; or char *name = temp;

this is trouble: short len = strlen (name) + 1; -your new array will be bigger than the old array, the pointer arithmetic will be wrong

1
2
do backward [x++] = name [--len];  //copy letter by leter from back to ahead  
    while (len != -1);  // do it until end of name 

ugh, put braces here. use cout to see the output, too.
hint: here postfix '++' and '--' mean something different

edit: size is not needed :s
Last edited on
okay from what i see it seems i totally gone into a wrong approach, if i want to use pointers to print a cstring backwards what's the correct approach?
You're making it too complicated.
You don't need to make a new string.

Use the same string and iterate through it backwards, starting with, in your case, x (i or iii is commonly used with newbies) as the length of your string and then decrementing it.

x = sizeof(name)/sizeof(name[0]); //common formula for array sizes

for(;x != 0;x--)
cout << name[x];
Last edited on
Ehh i am a bit confused what you mean billywilliam. I am required to use pointer and dynamically allocation to do this problem.
u mean
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

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



int main(void) 
{
	char temp[20];
	char *name = &temp;
	cout<<"input something";
	cin>>temp;
	//	char *name = temp;



	short len = strlen (name) + 1, x = -1;  //determine the size of "name"
    short size = len;
	char* backward = new char[len];
{
	do backward [x++] = name [--len];  //copy letter by leter from back to ahead  
    while (len != -1);  // do it until end of name

}
     


}

billywilliam wrote:
x = sizeof(name)/sizeof(name[0]); //common formula for array sizes


Except it leads to false/incorrect results for pointers/vectors/etc.

A better apporach is the below function:

1
2
3
4
5
6
7
8
9
10
11
12
// this function
template <unsigned S,typename T>
inline unsigned arraysize(const T (&v)[S]) { return S; }

#include <iostream>

int main()
{
  int foo[] = {1,2,3,4,5,6,7,8};

  std::cout << arraysize(foo);  // prints 8
}
Last edited on
I am in intermediate programming, just starting to learn pointers. I appreciate your help but i totally don't understand your first two lines "template <unsigned S,typename T>
inline unsigned arraysize(const T (&v)[S]) { return S; }
" and how come cout<<arraysize(foo); will print out 8? and what if i want to print out 8 7 6? or just 1 2 3?
ok after few times of testing i think i am a step closer check this out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cstring>
using namespace std;



int main(void) 
{
	char temp[20];
	cout<<"input something";
	cin>>temp;
	char* name = temp;  // pointer to string

	cout << "The second letter in name is " << *(name+1) << ".\n";
	
   


}


this code display the second element of what you input. E.G if u you keith it will cout "e" So bascially i can locate each letter, what i need now is a for loop that will display the whole name backwards, anyone got any idea. and if i do name+1 it will display eith.

My proposal is to use a link list that the last node point back to the first node, and somehow right a function to test which node is the first node then decrement it till the last node while writing a recursion to display it. my problem about my proposal is 1. i think i am overkilling it, 2. i don't know how to test which one is head node if i point my last node to the head node.
Last edited on
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
	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;
wait, hold on what is pIindex? it's a undeclared identifier as i see it. and why PIndex = szChar + pIndex;
when you say point my point to pIndex, you mean i char* name = Pindex? or i create a new one? If it's char* name, then that pointer will be pointing to pIndex AND temp which is invalid.
Last edited on
Is there any particular reason you are using an array to store a string, instead of String to store a string? You are using c with classes rather then c++. Almost all issues dissolve instantly when using the standard library.
I just posted a simple way to display a char array backwards in another thread, so I'll write the simple code out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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

int main()
{
         int      index;
         char   myName[32];

         cout << "Please enter name: ";
         cin.getline(myName, 32);

         for (index = strlen(myName) - 1; index >= 0; --index)
               {
               cout << myName[index];
               }
         cout << endl;
 
         return 0;
}
Last edited on
youre close with what you have. now that you can identify each individual letter using that pointer its simple to print them with a for that goes roughly like this:
1
2
3
4
for (int i = name.length(); name >= 0; name--)
{
	cout << *(letter + i);
}
Topic archived. No new replies allowed.