c++ myprogramminglab, ch.9 pointers and dynamic arrays, projects 70426 9.4: ====
ID: 657557 • Letter: C
Question
c++ myprogramminglab, ch.9 pointers and dynamic arrays, projects 70426
9.4:
====
Write a function that takes a C-string as a parameter and reverses the
string . The function will use two pointers to char , front and rear. The front
pointer will initially reference the first character in the string , and the rear
pointer will initially reference the last character in the string . Reverse the
string by swapping the characters referenced by front and rear pointers, then
increment the front pointer and decrement the rear pointer, and so on, until the
entire string is reversed.
Write a main program that would let the user input a string and then print
out the reversed version of the string .
For this exercise, you may assume the maximum length of a string is 100
characters .
SAMPLE RUN #1
--- Prompts For Keyboard/Console/Standard Input ---
Enter:
Inputs
--- Keyboard/Console/Standard Input stdin ---
0123456789
Outputs
--- Monitor/Console/Standard Output ---
Enter:a string :9876543210
What The Console Looks Like In An Interactive Session:
(Note: this combines standard input with standard output )
>./reverse_string
Enter: 0123456789
a string :9876543210
SAMPLE RUN #2
--- Prompts For Keyboard/Console/Standard Input ---
Enter:
Inputs
--- Keyboard/Console/Standard Input stdin ---
hello world
Outputs
--- Monitor/Console/Standard Output ---
Enter:a string :dlrow olleh
What The Console Looks Like In An Interactive Session:
(Note: this combines standard input with standard output )
>./reverse_string
Enter: hello world
a string :dlrow olleh
Explanation / Answer
#include<iostream>
#include<Cstring>
#include<algorithm>
int main()
{
int i,n;
string str,temp;
count<<"Enter the value of n"<<end;
cin<<n<<end;
count<<"Enter a string"<<end;
cin<<str<<end;
int length =strlen(str);
for(i=0;i<n;i++)
{
temp=reverse(str);
}
count<<"The reversed string is"<<temp<<end;
swap(str,length)
count<<"The swapped string is"<<str<<end;
}
void swap(char a[],int length)
{
char temp1;
char *front=a;
char *rear=a+length-1;
for(;front<=rear;++front,--rear)
{
temp1::swap(front,rear);
return swap;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.