Question

In: Computer Science

c++ I want to flip the string when it's string type. For example, if it is...

c++

I want to flip the string when it's string type.
For example, if it is 'apple', I would like to print 'elppa'.

how can i do?

Solutions

Expert Solution

Solution for the problem is provided below, please comment if any doubts:

There are a number of ways to flip a string in C++, based on your requirements you can use it, a function .reverse() is available under "algorithm" header, you can use pointers, using string iteration, etc. Also you can create a function to perform the job done.

The C++ program that flip a string using a defined function:

//add the header files
#include <iostream>
#include <string>
using namespace std;

//the function to perform the flip of a string
//Take thes string as reffernce to make the strinf flipped
void flipString(string &instr)
{
   //Find the length of the string
    int len = instr.length();

    /*flip the characters using a for loop
   //Flip the characters that are at same distance
   //from begining and end to make the flip
   */
    for (int i = 0; i < len / 2; i++)
   {
       //perform the dwap
        char temp =instr[i];
       instr[i] =instr[len - i - 1];
       instr[len - i - 1] = temp;
   }
}

//the main to check the function
int main()
{
   //test string
    string str = "apple";

   //perform the flip
    flipString(str);

   //output the flipped string
    cout << str<<endl;

    return 0;
}

Output:


Related Solutions

c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
I have a char memory[1024]. I want to write the string "mike" into the memory starting...
I have a char memory[1024]. I want to write the string "mike" into the memory starting from an index. I also need a function to move "mike" to another index in the same memory swapping m and k in mike. Basically, i need a function to write to memory and a function to move the string to an index. Thank you In c++ please.
When making aspirin I need to write the reaction, it's physical properties, and it's theoretical yield....
When making aspirin I need to write the reaction, it's physical properties, and it's theoretical yield. How do I find the theoretical yield?
I want to retire in 40 years. When I retire, I want to withdraw $20,000 every...
I want to retire in 40 years. When I retire, I want to withdraw $20,000 every 6 months from my savings for a period of 10 years. However, at the end of years two and three in my retirement, I will need to spend $40,000 and $60,000 respectively to send my grandson to culinary school. Assuming I can earn 4% compounded semi-annually, answer the following question: How much money will I need at year 40 to fund sending my grandson...
I want to compare a string with the previous strings in a forEach loop using javascript....
I want to compare a string with the previous strings in a forEach loop using javascript. So im accessing data from an API. Im iterating through them using a forEach() loop. I want to compare the current 'title' string from the api with all previous title strings its pulled. The title is located under article.title fetch(req)             .then((response) => {                 return response.json();             })             .then((data) => {                 console.log(data);                 data.articles.forEach((article) => { } If the title matches any other title used already id...
I want Example for Sign test for small sample
I want Example for Sign test for small sample
Step 1: What Do I Want to Be When I Grow Up? What do I want...
Step 1: What Do I Want to Be When I Grow Up? What do I want to be know for? If there was one thing I would like people to remember me for, what would that be? What should my epitaph say about me? When I retire, what I would like my most important accomplishment to be? Step 2: My Personal Mission Based on the results of the self-assessment about values and the answers to the preceding questions, write your...
I want the code below to be edited: Rather than giving the input string inside the...
I want the code below to be edited: Rather than giving the input string inside the code, I want the program to ask the user for an input and calculate and complete this code. I have pasted the actual code below, Please edit the input section only so that I can input any string or any sentence as I like. The program must ask the user that "Enter a string/sentence" and take the data to calculate the Huffman code. #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT