forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_string.cpp
51 lines (45 loc) · 1.42 KB
/
reverse_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
PROBLEM STATEMENT:
Given a string, The task is to print its reverse. The approach is to traverse half of the string and swapping each character with a subsequent character present
in the other half of the string. For example, if the given string is "computer" then, we'll traverse the string till 'p' and swapping will be performed in the following way:
c with r
o with e
m with t
p with u
At the end, we'll get "retupmoc", which is the required answer.
We can also reverse a string using a predefined function of STL in CPP i.e., std::reverse()
SYNTAX: reverse(str.begin(), str.end());
*/
#include<bits/stdc++.h>
using namespace std;
//A function to reverse a string and print it
void revstr(string str){
//size() is used to find the length of the string
int len = str.size();
//Traversing half of the string
for(int i = 0; i < len/2; i++){
//swapping
swap(str[i], str[len - 1 - i]);
}
//Printing reversed string
cout<<str;
}
int main(){
string str;
//Taking input using getline() to include whitespaces
getline(cin, str);
//A function call to reverse a string
revstr(str);
return 0;
}
/*
TEST CASES:
1.
Input: Hello World
Output: dlroW olleH
2.
Input: What is your name???
Output: ???eman ruoy si tahW
TIME COMPLEXITY: O(n), due to one for loop used for traversing the string, where 'n' denotes the length of the string.
SPACE COMPLEXITY: O(1), no extra space used
*/