Put spaces between words starting with capital letters

Configurare noua (How To)

Situatie

You have a sentence. However there is no space between different words and the first letter of every word is in uppercase.

For example:

Input : BruceWayneIsBatman

Output : bruce wayne is batman

Solutie

// C++ program to put spaces between words starting
// with capital letters.
#include <iostream>
using namespace std;
 
// Function to amend the sentence
void amendSentence(string str)
{
    // Traverse the string
    for(int i=0; i < str.length(); i++)
    {
        // Convert to lowercase if its
        // an uppercase character
        if (str[i]>='A' && str[i]<='Z')
        {
            str[i]=str[i]+32;
 
            // Print space before it
            // if its an uppercase character
            if (i != 0)
                cout << " ";
 
            // Print the character
            cout << str[i];
        }
 
        // if lowercase character
        // then just print
        else
            cout << str[i];
    }
}
 
// Driver code
int main()
{
    string str ="BruceWayneIsBatman";
    amendSentence(str);
    return 0;
}

Output: bruce wayne is batman

Tip solutie

Permanent

Voteaza

(8 din 24 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?