Hey guys. This is a code that can revese a string by word. But i can't know how it take each individual word.As you see, we have 2 pointer head and tail. Tail point to the lastest individual word in char arry and another to find the head of each word. But why it can print out the whole word instead a char in this case ? Would somebody explant it to me? Thanks alot
Thanks for your help but i'm not clearly know it all. In example, i've modified my code in to this one
1 2 3 4 5 6 7 8 9 10 11 12 13
int main(int argc, char **argv)
{
char input[255];
char *head;
char *tail;
cout<<"Plz enter a string =.="<<endl;
cin.getline(input, 255,'\n'); // read a whole line.
tail = input + strlen(input)-1; // if you can use <cstring>?
head=tail-4;
cout<<head;
cout << endl;
return 0;
}
and run debug and enter this string:"Final Fantasy" and the output it's "ntasy". In code, tail point to "y" so head=tail-4 ---> head point to "n" so i think that this line : cout<<head =n. But why the output it's "ntasy". It makes me so confuse .
well, head is a pointer to char. The stream/cout interprets a pointer to char as a string hence everything is printed until 0 is reached.
If you want to see the 'n' only you need to cout<<head[0]; or cout<<*head; // Note *
As soon as it becomes a pointer the whole string is printed: cout<<&head[1];
will print "tasy"