- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the direction from given string in C++
Suppose we have a string which contains only L and R, this denotes left rotation and right rotation respectively, we have to find the final direction of pivot. Here directions are north(N), east(E), south(S) and west(W). We are assuming that the pivot is pointed towards north(N) in a compass.
So, if the input is like "RRLRLLR", then the output will be E, as initial direction is N, RR will point to S, then LR will point to the same N again, then LL will point to previous position N, then R will point to E. So E is the final.
To solve this, we will follow these steps −
count := 0
direction := a blank string
for initialize i := 0, when i − length of s, update (increase i by 1), do −
if s[i] is same as 'L', then −
(decrease count by 1)
Otherwise
(increase count by 1)
if count > 0, then −
if count mod 4 is same as 0, then −
direction := "N"
otherwise when count mod 4 is same as 1, then −
direction := "E"
otherwise when count mod 4 is same as 2, then −
direction := "S"
otherwise when count mod 4 is same as 3, then −
direction := "W"
if count < 0, then −
if count mod 4 is same as 0, then −
direction := "N"
otherwise when count mod 4 is same as -1, then −
direction := "W"
otherwise when count mod 4 is same as -2, then −
direction := "S"
therwise when count mod 4 is same as -3, then −
direction := "E"
return direction
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; string get_dir(string s) { int count = 0; string direction = ""; for (int i = 0; i < s.length(); i++){ if (s[0] == '\n') return NULL; if (s[i] == 'L') count--; else count++; } if (count > 0){ if (count % 4 == 0) direction = "N"; else if (count % 4 == 1) direction = "E"; else if (count % 4 == 2) direction = "S"; else if (count % 4 == 3) direction = "W"; } if (count < 0){ if (count % 4 == 0) direction = "N"; else if (count % 4 == -1) direction = "W"; else if (count % 4 == -2) direction = "S"; else if (count % 4 == -3) direction = "E"; } return direction; } int main() { string s = "RRLRLLR"; cout << (get_dir(s)); }
Input
"RRLRLLR"
Output
E
- Related Articles
- C++ code to find minimum correct string from given binary string
- C++ code to find reduced direction string of robot movement
- Program to find a good string from a given string in Python
- Find sub-string with given power in C++
- Remove all duplicates from a given string in C#
- Count possible moves in the given direction in a grid in C++
- Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++
- Find the ordering of tasks from given dependencies in C++
- Remove a Given Word from a String using C++
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Program to find next board position after sliding the given direction once in Python
- Find if a string starts and ends with another given string in C++
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- How to delete the vowels from a given string using C language?
- Program to balance the direction string so that each direction occurred quarter times in Python
