Rawash
Rawash

Reputation: 1

Trying to write a code the corrects a string sentence that is written inside out

I'm a software engineering student, and i need some help with an assignment i was given i need a code that corrects a sentence written inside out example; i love programming rp evol i gnimmargo

(what i mean by inside out .... that the sentence gets cut into half and each half is flipped) i need to correct the scrambled sentence

i already started it by counting the charterers in the string that the user enters and cutting the sentence .... but i just cant figure out how to flip each half then join them any ideas??

#include <iostream>
#include <string>
using namespace std;

//prototype
int NumberOfChar(string testCase);

int main()
{
    // declaration

    int N,  halfCharNum, halfTestCase, size;
    string testCase;

    // input

    cout<<"please enter an integer number, that will represent the number of test cases:      "<<endl;
//cin>>N;

    cout<< NumberOfChar(testCase)<<endl;

    halfCharNum=size/2;

    return 0;
}

int NumberOfChar(string testCase)
{
    cout << "Enter your string: " <<endl;
    getline(cin,testCase);
    const int size=testCase.length();
    cout << "The total number of characters entered is: "  << endl;
    return size;
}

Upvotes: 0

Views: 102

Answers (3)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

This takes all of two lines of C++ code.

1) reverse the string (std::reverse)

2) Rotate the string left n characters where n is half the number of characters in the string. (std::rotate)

Upvotes: 2

Jay
Jay

Reputation: 6638

Just use a large dictionary of english words and implement an evolutionary algorithm. Might not be the shortest path to glory but definitely a fun task :-)

Sample dictionaries (you might need anyway) can be found

Upvotes: 0

Travis
Travis

Reputation: 321

First, replace each element in the string with the element on the other end and work your way in.

i.e. in the string "rp evol i gnimmargo" exchange the first character "r" with the last character "o" and then work your way in, next exchanging "p" with "g" and so on.

That leaves you with "ogramming i love pr"

Then, swap the first and second halves of the string.

i.e. "ogramming i love pr" can be split into "ogramming" and " i love pr" -- just swap them for " i love pr" and "ogramming" and combine them

Upvotes: 0

Related Questions