Change the string | Practice | GeeksforGeeks

Given a string S, the task is to change the complete string to Uppercase or Lowercase depending upon the case for the first character.
Example 1:
Input:
S = "abCD"
Output: abcd
Explanation: The first letter (a) is 
lowercase. Hence, the complete string
is made lowercase.


â€‹Example 2:
Input: 
S = "Abcd"
Output: ABCD
Explanation: The first letter (A) is
uppercase. Hence, the complete string
is made uppercase.

Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes the string S as input and returns the resultant string with stated modifications.

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1) for C++ and O(|S|) for output in Java/Python.

Constraints:
1<=|S|<=104
 
0
akash22bhin 6 hours
Total time taken-0.01/1.04
string modify (string s)   {       // your code here       if(s[0]>='a'&&s[0]<='z')       {           for(int i=1;i<s.length();i++)           {               if(s[i]>='A'&&s[i]<='Z')               s[i]+=32;           }       }       if(s[0]>='A'&&s[0]<='Z')       {           for(int i=1;i<s.length();i++)           {               if(s[i]>='a'&&s[i]<='z')               s[i]-=32;           }       }       return s;   }
0
gurankitbehal781141 week ago
class Solution{   public:      string modify (string s)   {       if(s[0]>='A' && s[0]<='Z'){         for(int i=0;i<s.length();i++){             s[i]=toupper(s[i]);         }         }         if(s[0]>='a' && s[0]<='z'){         for(int i=0;i<s.length();i++){             s[i]=tolower(s[i]);         }         }         return s;   }};
0
mayank180919991 week ago
 string modify (string s)
    {
        // your code here
         string v;
            if(islower(s[0])){
               for(int i=0;i<s.length();i++){
                   v.push_back(tolower(s[i]));
               } 
            }else{
                for(int i=0;i<s.length();i++){
                   v.push_back(toupper(s[i]));
               } 
            }
        return v;
    }
0
vermahariom10002 months ago
class Solution{   String modify(String s)   {       if(s.charAt(0)>='a' && s.charAt(0)<='z')       return s.toLowerCase();              else       return s.toUpperCase();          }}
0
chhitizgoyal2 months ago
Java Solution. 
class Solution{   String modify(String s){       if(s.charAt(0)>=65 && s.charAt(0)<=90){        return s.toUpperCase();          }       return s.toLowerCase();       }}
0
1ashishchauhan20022 months ago
    string modify (string s)   {     if(s[0]>=65 && s[0]<=90){         transform(s.begin(), s.end(), s.begin(), ::toupper);     }     if(s[0]>=97 && s[0]<=122){       transform(s.begin(), s.end(), s.begin(), ::tolower);     }     return s;   }
 
+1
shubhamkhavare2 months ago
class Solution{   String modify(String s){       if (Character.isUpperCase(s.charAt(0)))       {           return s.toUpperCase();       }       return s.toLowerCase();   }}
+1
sdmrf2 months ago
Python :
class Solution:   def modify(self, s):       if s[0].isupper()==True:           return s.upper()       else:           return s.lower()
0
yash7raut2 months ago
class Solution:   def modify(self, s):       # code here       x = s[0].isupper()       if(x == True):           s = s.upper()       else:          s =  s.lower()              return(s)   
+1
as0042303 months ago
In C++;
  string modify (string s)   {       string str = "";       for(int i = 0; i < s.size(); i++)       {           if(islower(s[0]))               str += tolower(s[i]);           else               str += toupper(s[i]);       }       return str;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.