my code is running correctly in normal strings input but when i give aprox more than 34 length strings it gives runtime error on questn of codeforces .. link to questn http://codeforces.com/problemset/problem/112/A

codeforces cf-112a

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

int main()
{
string s1,s2,s3,s4;
int len,i;
cin>>s1>>s2;
len=s1.length();

 for(i=0;i<len;i++)
{
 if(isupper(s1[i]))
    s3[i]=tolower(s1[i]);

 else if(islower(s1[i]))
    s3[i]=s1[i];

 if(isupper(s2[i]))
   s4[i]=tolower(s2[i]);

 else if(islower(s2[i]))
    s4[i]=s2[i];

 }

 int c=0;
 for(i=0;i<len;i++)
 {
      if((s3[i]-'a')>(s4[i]-'a'))
         {
             c=1;
             break;
         }

      if((s3[i]-'a')<(s4[i]-'a'))
         {
             c=2;
             break;
         }


 }


      if(c==0)
            cout<<"0";
      else if(c==1)
            cout<<"1";
      else if(c==2)
            cout<<"-1";




return 0;
}

 

manish_kumar
Manish Kumar Savita
manish_kumar

Please Log in to answer

Note: Your answer should not be too short. Please wait a few seconds for the editor to load



Please make sure the answer is not too short

1 Answer

preda2or
preda2or 17:31, Mar 24
Shubham Kumar Gupta

for(i=0;i<len;i++)
{
 if(isupper(s1[i]))
    s3[i]=tolower(s1[i]);

 else if(islower(s1[i]))
    s3[i]=s1[i];

 if(isupper(s2[i]))
   s4[i]=tolower(s2[i]);

 else if(islower(s2[i]))
    s4[i]=s2[i];

 }

This part of your code is causing segmentation fault. Strings are stored like arrays. You have declared s3 and s4 but you can't index them until you store something in them.  After taking s1 and s2 as input just initialize s3 with s1 and s4 with s2

s3=s1;

s4=s2;

Now you are good to go.

Also when using cin,cout include this line in your code for faster input/output.(Not necessary for this problem though)

std::ios_base::sync_with_stdio(false);

 




Please make sure the answer is not too short
1 Upvotes
Comments
manish_kumar (Manish Kumar Savita) 16:22, Mar 25

got the previous error too:)

manish_kumar (Manish Kumar Savita) 16:22, Mar 25

i have made changes in this code now i am storing directly in s1 and s2 ,no need of s3 and s4 and0 its working