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...
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...
For C++ Assume that word is a variable of type string that has been assigned a...
For C++ Assume that word is a variable of type string that has been assigned a value. Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared, drWord, also of type string. Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT