Write a segment of C++ code that, given a string inStr, will create a new string
ID: 3924375 • Letter: W
Question
Write a segment of C++ code that, given a string inStr, will create a new string outStr such that outStr is the reverse of intStr. Use a loop. For example, if the given inStr is “Hello” then your code should create outStr equal to “olleH”.
(This is only an example; your code must be general and must work for any given input string inStr.)
Hint: Use a loop to extract the characters from inStr, one at a time, and use them to build up by concatenating them using the + operator.
Write a segment of C++ code that, given a string inStr, will create a new string outStr such that outStr is the reverse of intStr. Use a loop. For example, if the given i
Explanation / Answer
include<iostream>
#include<conio.h>
#include<string>
int main()
{
std::string arr;
int i =0,len,a;
std::cout<<"Enter the String: ";
getline (std::cin, arr);
std::cout<<" ";
std::cout<<"String You Have Entered Is: "<<arr;
int j=arr.length()-1;
while(i<j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
std::cout<<" ";
std::cout<<"Reverse of the String is :"<<arr;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.