dimanche 25 octobre 2020

Writing a code which converts the string of time given to 24-hr format(some people call it military time)

So the basic idea is to covert a given string like 12:00:00AM to 0:00:00(without am or pm).So basically the idea is that we have to add the digits by 12 if its for PM and it remains as it is for AM.

Except for 12 AM and 12PM.Sorry if my code does not make sense,and pl tell me in that case.If you can fix the error for me it would be best otherwise can you provide me better solution for the problem ??.

string timeConversion(string s) {
               int a = s.length();
               string t;
    
               if(s[a-1]=='M' && s[a-2]=='P'){             
                 for(int i=2;i<a-2;i++){
                   t[i] == s[i]; // error-value computed is not used                                               
                 }
                 if(s[0]+s[1] != 12){   
                 t[0]+t[1]=12+(s[0]+s[1]); //error-lvalue required as left operand of assignment 
                 }
                 if(s[0]+s[1] == 12){
                     t[0]+t[1]==(s[0]+s[1]); //error-value computed is not used
                 }
               }
               else{
               if(s[a-1]=='M' && s[a-2]=='A'){
                   if(s[0]+s[1]==12){
                 t[0]+t[1]=12+(s[0]+s[1]);
                   }
                   else{
                       t == s;
                   }
               }
               }
    
          return t;
}

Aucun commentaire:

Enregistrer un commentaire