#include<iostream>
#include<cstring>
usingnamespace 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 = newchar[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?
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
#include<iostream>
#include<cstring>
usingnamespace 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 = newchar[len];
{
do backward [x++] = name [--len]; //copy letter by leter from back to ahead
while (len != -1); // do it until end of name
}
}
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>
usingnamespace 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.
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:
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.
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.
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);
}