Saturday, 22 January 2022

Reverse String

Write a function that takes a string as input and reverse the same.
Example:
Given s = "hello", return "olleh".
Approach: The idea is to swap the last and the first characters till the middle is reached.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
string reverseString(string &s) {
    int low = 0, high = s.length() - 1;
    while(low < high){
        char t = s[low];
        s[low] = s[high];
        s[high] = t;
        low++;
        high--;
    }
    return s;
}  
Share:

2 comments:

  1. Impressive posting, really liked reading it. I like your writing style, it’s quite unique. Thanks for sharing the information here. Become a Certified DevOps Expert with Comprehensive DevOps Training

    ReplyDelete
  2. "Your blog is a treasure trove of knowledge! The way you present ideas with such clarity and enthusiasm is truly inspiring. I’m excited to see what you’ll share next—keep up the amazing work!"

    ReplyDelete

Contact Me

Name

Email *

Message *

Popular Posts