//--------------------------------------------------------------------
// strprint.cpp
//--------------------------------------------------------------------

// Uses a char pointer to move through a string character by character.
#include <iostream.h>
void main() { char heading[11] = "A title", // Sample string *strPtr; // Pointer to a character in // the string
// Set strPtr to point to the first character in the string. strPtr = heading;
// Display each character in the string. Stop when strPtr points // to the null character at the end of the string. while ( *strPtr != '\0' ) { cout << *strPtr; // Output the character pointed to by strPtr strPtr++; // Advance to the next character } cout << endl;
// Pointer strPtr points to the null character at the end of the // string. Move strPtr back one character so that it points to // the last (non-null) character in the string. strPtr--;
// Display the characters in the string in reverse order. // Stop when strPtr moves past the beginning of the string. while ( strPtr >= heading ) { cout << *strPtr; // Output the character pointed to by strPtr strPtr--; // Move back one character } cout << endl; }