Write a function that accepts a pointer to a Cstring as an a

Write a function that accepts a pointer to a C-string as an argument and displays its contents backward. For instance, if the string argument is ‘Gravity’ the function should display ‘ytivarG’ Demonstrate the function in a program that asks the user to input a string and then passes it to the function.

Solution

//-------------Start of Program-------------> #include #include using namespace std; //Function Prototype void stringLength(char *); void reverseString(char *); //----------------Start Main Function-------------------> int main() { const int INPUT_SIZE = 80; char input[INPUT_SIZE]; //Get the user\'s desired string cout << \"Please enter a phrase or sentence: \ \ \"; cin.getline(input, INPUT_SIZE); //Read input as a string //Display number of characters cout << \"\ The entered string is \"; stringLength(input); cout << \" characters. \ \"; //Display string backwards cout << \"The entered string backwards is: \"; reverseString(input); cout << endl; } //<------------End Main Function-----------------------> //<------------Start String Length Function------------> void stringLength(char *string1) { int stringCount = 0; stringCount = strlen(string1); cout << stringCount; } //<------------End String Length Function--------------> //<------------Start Reverse String Function-----------> void reverseString(char *string2) { char *revString = string2; while(*revString != \'\\0\') ++revString; while (revString != string2) cout.put(*--revString); } //<-----------End Reverse String Function-------------->
Write a function that accepts a pointer to a C-string as an argument and displays its contents backward. For instance, if the string argument is ‘Gravity’ the f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site