/* ************************************************************************** * Program name : 052_String_in_reverse_order(pointer) (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/07/06 - first version * ************************************************************************** * Description : (a) Introduction * * (b) Asking the user to input a string * * (c) Print the string forward and backward * * (d) Try again ? * ************************************************************************** */ #include #include #include #include int main() { // part 1 : declaration const int Size = 31; char InputString[Size]; char *p; int Index, Length; char Again; // part 2 : Introduction cout << "\n\n\t\tString_in_reverse_order (Version 1.00)\n" << "\n\tPlease input a string (without space and less than 30 characters" << "\n\tlong), then this program will print that string backword."; do { // part 3 : Asking the user to input a string cout << "\n\n\tYour string is : "; cin >> InputString; cout << "\t----------------------------------------------------"; // part 4 : Ouput result p = InputString; // Note(1) cout << "\n\tDisplay in reverse order : "; // Note(2) for (Index=strlen(p)-1; Index>-1; Index--) cout << p[Index]; cout << "\n\tConverted to upper case : "; // Note(3) for (Index=0; Index<=strlen(p)-1; Index++) cout << char(toupper(p[Index])); cout << "\n\tConverted to lower case : "; // Note(3) for (Index=0; Index<=strlen(p)-1; Index++) cout << char(tolower(p[Index])); // part 5 : try another number ? cout << "\n\n\t\aTry another number (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Note (1) The pointer p has been set to the address of the first element in the array InputString. (2) There are four different ways to get the value in an array : Arrayname[Element offset] e.g. InputString[3] *(Arrayname+offset) e.g. *(InputString+3) Pointername[Element offset] e.g. p[3] *(Pointername+offset) e.g. *(p+3) (3) (a) The functions toupper and tolower (needs the statement #include ctype.h) returns an integer value of the character after converting to uppercase or lowercase letter. Example : tolower('A') = 97 , char(97) = 'a' toupper('a') = 65 , char(65) = 'A' (b) Sample output : Your string is : PeterWong ------------------------------------ Display in reverse order : gnoWreteP Converted to upper case : PETERWONG Converted to lower case : peterwong */