Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 1.21 KB

amend-the-sentence.md

File metadata and controls

46 lines (38 loc) · 1.21 KB

Amend The Sentence

Problem Link

Given a string which is basically a sentence without spaces between words. However the first letter of every word is in uppercase. You need to print this sentence after following amendments:

  1. Put a single space between these words
  2. Convert the uppercase letters to lowercase.

Note: The first character of the string can be both uppercase/lowercase.

Sample Input

BruceWayneIsBatman

Sample Output

bruce wayne is batman

Solution

class Solution {
    public:
    string amendSentence (string s)
    {
        string ans = "";
        for(char c: s) {
            if(c >= 'A' && c <= 'Z') {
                if(ans.size()) {
                    ans += ' ';
                }
                ans += (c - 'A' + 'a');
            }
            else {
                ans += c;
            }
        }
        return ans;
    }
};

Accepted

image