vishal_khanna
vishal_khanna

Reputation: 57

Unexpected output in every case.What is wrong with my solution?

Problem 131A-http://codeforces.com/problemset/problem/131/A My solution instead of doing what the problem asks,returns the same string which is inputted.What is wrong with my solution.

My solution:

#include<iostream>
#include<cstring>
using namespace std;
int i,a;
char input[101];
int main()
{
    cin>>input;
    a=0;
    for(i=0;input[i]!=NULL;i++) //To find number of uppercase letters.
    {
        if(isupper(input[i]))
        a++;
    }
    if((a==strlen(input))|((a==(strlen(input)-1))&(islower(input[0])))) //To check for accidental pressing of Caps Lock(Words like Jingle or jINGLE.).
    {
        toupper(input[0]);
        for(i=1;input[i]!=NULL;i++)
        {
            tolower(input[i]);
        }
        cout<<input;
    }
    else
    cout<<input;
}

Upvotes: 0

Views: 70

Answers (3)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

char toupper(char) and char tolower(char) only returns converted character, and doesn't modify passed one (it would require reference or pointer usage). Instead use:

input[0] = toupper(input[0]);
for(i=1;input[i]!=NULL;i++)
{
    input[i] = static_cast<char> ( tolower( static_cast<unsigned char>(input[i]) ) );
}
cout<<input;

Upvotes: 1

Useless
Useless

Reputation: 67713

toupper(input[0]);

doesn't change input[0] to upper case, it returns the upper-case equivalent of input[0].

Try

input[0] = toupper(input[0]);

instead (and the same for every other toupper/tolower call).

Upvotes: 1

mb84
mb84

Reputation: 693

toupper() or tolower() don't change their argument, but return the changed char. You must write input[i] = toupper(input[i]). Then you shouldn't do theese conditional checks with bitwise operators. Use || and && instead.

Upvotes: 1

Related Questions