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...
upload cvs file dialog it's sub menu bar and when I click this I want to...
upload cvs file dialog it's sub menu bar and when I click this I want to popup file dialog and select cvs file <a href="#" id="loadCSV">Load CSV file</a>
In c++ Write a function that: accepts a string and an integer as it's only arguments...
In c++ Write a function that: accepts a string and an integer as it's only arguments uses the argument to open a file for writing writes the integer to the file if it fails to open the file, returns -1 if it succeeds in opening the file, returns 0 does not interact with the user in any way does not use global variables
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...
when i type those python code below, i got True, but when i type not a...
when i type those python code below, i got True, but when i type not a == e or d and not c > b i still get true, why is that?  What will be the returned data type? a = 1 b = 2 c = 3 d = True e = 'cool' a == e or d and c > b
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 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.
1) If c is type char and i type int, what is assigned to c and...
1) If c is type char and i type int, what is assigned to c and i? Give answers in hex. Assume c is one byte, and i is four bytes. Explain. c = 240; i = c; 2) If c is type unsigned char and i type int, what is assigned to c and i? Give answers in hex. Explain c = 240; i = c; 3) If c is type signed char and i type int, what is...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT