Answer to Question #250751 in C++ for Gayatri

Question #250751
A prisoner receives an uplifting and encouraging message from on e of his friends He uses the Chirp App to communicate with him A they exchange messages, he receives a parcel containing seme gifts with a sienes of characters labelled on it The string consists of only y Ls and Rs Deep in thoughts, he tries to understand the mean ng on the laber. He finally deciphers it as directions for hum to esca pe from the prison Given st, the task here is to find the final cardin al direction(NWES he anives when he moves according to the di recons
Asstime, the prisoner always starts his movement
LLeft direction on from North(N)
Example 1: Input:
LLR
-Value of str
Output:
-Final cardinal direction
W
Explanation:
From the input given above
As the prisoner starts from Narmo)
1
Expert's answer
2021-10-13T06:25:20-0400


#include <iostream>
using namespace std;
 


string cardinalDirection(string str1)
{
    int c = 0;
    string direction = "";
 
    for (int i = 0; i < str1.length(); i++) {
 
        if (str1[0] == '\n')
            return NULL;
 
        if (str1[i] == 'L')
            c--;
        else {
            if (str1[i] == 'R')
                c++;
        }
    }
 
    
    if (c > 0) {
 
        if (c % 4 == 0)
            direction = "N";
        else if (c % 4 == 1)
            direction = "E";
        else if (c % 4 == 2)
            direction = "S";
        else if (c % 4 == 3)
            direction = "W";
    }
 
   
    if (c < 0) {
 
        if (c % 4 == 0)
            direction = "N";
        else if (c % 4 == -1)
            direction = "W";
        else if (c % 4 == -2)
            direction = "S";
        else if (c % 4 == -3)
            direction = "E";
    }
    return direction;
}
 
int main()
{
    string str = "LLR";
    cout << (cardinalDirection(str)) << endl;
 
    
}
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS