Invert Case of Character

You are given a string S followed by two integers N1 and N2. You need to change the case of character at those two indices as specified by the two integers.

Example:

Input:  s = "Jamia Hamdard", n1 = 2, n2 = 9
Output: JAmia HaMdard

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string invertCaseOfCharacters(string sint n1int n2)
{
    for (int i = 0i < s.size(); i++)
    {
        if (i + 1 == n1 || i + 1 == n2 && s[i] != ' ')
        {
            if (s[i] >= 'A' && s[i] <= 'Z')
                s[i] = s[i] + 32;
            else if (s[i] >= 'a' && s[i] <= 'z')
                s[i] = s[i] - 32;
        }
    }
    return s;
}
int main()
{
    string s = "Jamia Hamdard";

    int n1 = 2n2 = 9;

    cout << invertCaseOfCharacters(sn1n2<< "\n";

    return 0;
}


No comments:

Post a Comment