The judge said wrong answer in cAPS lOCK

  c++, conditional-operator

everyone. In the question cAPS lOCK on Codeforces, the judge said the wrong answer for test case number 3, i.e, ‘cAPSlOCK’. Now, from reading the question and the following conditions being given:

"Let’s consider that a word has been typed with the Caps lock key accidentally switched on, if:

a) either it only contains uppercase letters; b) or all letters except for the first one are uppercase."

taking the above conditions into consideration, ‘cAPSlOCK’ should change to ‘CapsLock’ as apart from ‘c’ not all the words are lowercase, i.e, ‘l’.

#include <iostream>
#include<string>
#include <iterator>
using namespace std;

int main()
{

string str;
cin>>str;
int count(0);
int len = str.length();
bool flag = true;
char first{str[0]};

if(isupper(first))
{
    for(int i = 1; i<len; i++)
    {
        if(islower(str[i]))
        {
            count++;
        }
    }
}

count = count;


if (count == len - 1){flag = false;}
count = 0;

if(len = 1 && islower(first)){flag = true;}

while(flag)
{
for(auto& i : str)
    {
        if(isupper(i))
        {
            i = tolower(i);
        }
        else if (islower(i))
        {
            i = toupper(i);
        }
    }
break;
}

cout<<str<<endl;

}

So, my code converts ‘cAPSlOCK’ to ‘CapsLock’, however the desired answer is ‘cAPSlOCK’

I highly appreciate anyone’s input here.

Source: Windows Questions C++

LEAVE A COMMENT