rob_tgrs's blog

By rob_tgrs, history, 9 days ago, In English,

this is related to problem 131A , my code is giving correct output on devc++ but codeforces is giving wrong answer on the 1st test case.if there would have been any run time error or so ,it should have given something else instead of wrong answer. i am a beginner at coding and so dont know how todeal with it.please help . i am attaching code and different output of dev c++ and codeforces.

include<bits/stdc++.h>

using namespace std;

int main()

{ unsigned int cnt1,i;

string s1;

cin>>s1;

std::string::iterator it;

for(it=s1.begin();it!=s1.end();it++)

{ if(isupper(*it))

cnt1++;

}

if(islower(s1[0])&&cnt1==s1.size()-1)

{

putchar(toupper(s1[0]));

for(i=1;i<s1.size();i++)

{ putchar(tolower(s1.at(i)));

} } else if(isupper(s1[0])&&cnt1==s1.size())

{

for(i=0;i<s1.size();i++)

{ putchar(tolower(s1.at(i))); }

} else

cout<<s1;

return 0;

}

input:cAPSLOCK

dev c++ output:Capslock

codeforces :cAPSLOCK and hence giving wrong answer

 
 
 
 
  • Vote: I like it  
  • -18
  • Vote: I do not like it  

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by rob_tgrs (previous revision, new revision, compare).

»
9 days ago, # |
  Vote: I like it +3 Vote: I do not like it

The variable cnt1 seems to be uninitialized.

Try to initialize it to zero if you are using it as a counter.

Best wishes

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thanks.it worked.

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      With pleasure.

      The following is a little more readable code for the problem that may be helpful in your first few steps in coding.

      33011483

      Best wishes