Wednesday, December 25, 2024
Google search engine
HomeData Modelling & AIHow to create half of the string in uppercase and the other...

How to create half of the string in uppercase and the other half in lowercase?

The first thing that we have to keep in my mind while solving this kind of problem is that the strings are immutable i.e,

If I am taking the following string

var string1 = "neveropen";
string1[0] = "G";

console.log(string1);

As strings are immutable, so we cannot change the character of the string, so the output of the above code will be the following.

Output:

neveropen

Approach:

There are several ways to solve this problem. Some of them are as follows.

  • Splitting the string into 2 different parts and changing the first part to the UPPERCASE and concatenating the new strings to get the output.
  • Create an empty string and add each character of the string to it using FOR loop.
  • Using the slicing property of strings to get the output.

Method 1: This method is implemented using 2 new variables.

  • Add the string into the variable.
  • Store the length of the string into a variable using string.length function.
  • Create two empty strings which are used in the future to store the newly created strings.
  • Use of for loops to traverse in the string.
  • Convert the first string to the upper case using toUpperCase().
  • Concatenate both of the strings to get the output.

C++




#include <iostream>
using namespace std;
 
int main() {
  string str = "neveropen",ans;
  int len = str.length();
   
  for(int i = 0;i<len;i++){
    if(i<=len/2){
      char a = toupper(str[i]);
      ans.push_back(a);
    }else
      ans.push_back(str[i]);
     
  }
  cout<<ans;
  return 0;
}
 
// This code is co ntributed by rohitsingh07052


Python3




import math
 
string1 = 'neveropen'
string1_len = len(string1)
part_a = ''
part_b = ''
 
for i in range(int(math.ceil(string1_len // 2 + 1))):
    part_a += string1[i]
 
for i in range(int(math.ceil(string1_len // 2)) + 1,
               string1_len):
    part_b += string1[i]
 
new_part_a = part_a.upper()
 
changed_string = new_part_a + part_b
 
print(changed_string)
 
# This code is contributed by ukasp


Javascript




<script>
var string1 = 'neveropen';
var string1_len = string1.length;
var part_a = '';
var part_b = '';
 
for(var i=0 ; i<Math.ceil(string1_len/2) ; i++)
{
    part_a+=string1[i];
}
 
for(var i=Math.ceil(string1_len/2) ; i<string1_len ; i++)
{
    part_b+=string1[i];
}
 
var new_part_a = part_a.toUpperCase();
 
var changed_string = new_part_a + part_b;
 
console.log(changed_string);
</script>


Output

GEEKSFOrneveropen

Time Complexity: O(N)
Auxiliary Space: O(1)

Method 2: This method is implemented using a single new variable.

  • Add the string into the variable.
  • Store the length of the string into a variable using string.length function.
  • Create an empty string that is used in the future to store the newly created string.
  • Use of for loops to traverse the string.
  • Log the final string to get the output.

C++




#include <iostream>
using namespace std;
 
string convertFun(string& s1, int n)
{
    string ans = "";
 
    // Running loop on first half and making it
    // upper case
    for (int i = 0; i < n / 2; i++) {
        ans += toupper(s1[i]);
    }
 
    // Running loop on secone half
    for (int i = n / 2; i < n; i++) {
        ans += s1[i];
    }
 
    return ans;
}
 
// Driver code
int main()
{
    string s1 = "neveropen";
    // Finding length of string
    int n = s1.size();
 
    cout << convertFun(s1, n);
 
    return 0;
}
 
// This code is added by Arpit Jain


Javascript




<script>
var string1 = 'gfg';
var string1_len = string1.length;
var changed_string = '';
 
for(var i=0 ; i<Math.ceil(string1_len/2) ; i++)
{
    changed_string+=string1[i].toUpperCase();
}
 
for(var i=Math.ceil(string1_len/2) ; i<string1_len ; i++)
{
    changed_string+=string1[i];
}
 
console.log(changed_string);
</script>


Python3




import math
 
string1 = 'gfg';
string1_len = len(string1)
 
changed_string = ''
 
for i in range(math.ceil(string1_len/2)):
    changed_string += string1[i].upper();
 
for i in range(math.ceil(string1_len/2), string1_len):
    changed_string += string1[i]
 
print(changed_string)
 
# This code is contributed by avanitrachhadiya2155


Output

GEEKSForneveropen

Time Complexity: O(N)
Auxiliary Space: O(1)

Method 3: This method is implemented using the JavaScript Slice property.

  • Add the string into the variable.
  • Store the length of the string into a variable using string.length function.
  • Store the ceil value of half of the length of the string to the new variable.
  • Create 2 empty strings which are used in the future to store the newly created strings.
  • Add string to the variables using string slicing property.
  • Convert the first string to the upper case using toUpperCase().
  • Concatenate both of the strings to get the output.

Python3




import math
 
string1 = 'neveropen'
 
string1_len = len(string1)
 
half_string = math.ceil(string1_len/2)
 
part_a = ''
part_b = ''
 
part_a = string1[:half_string]
new_part_a = part_a.upper()
 
part_b = string1[half_string:string1_len]
 
changed_string = new_part_a+part_b
 
print(changed_string)
 
# This code is contributed by rag2127


Javascript




<script>
var string1 = 'neveropen for neveropen';
var string1_len = string1.length;
var half_string = Math.ceil(string1_len/2);
var part_a;
var part_b;
 
part_a = string1.slice(0,half_string);
var new_part_a = part_a.toUpperCase();
part_b = string1.slice(half_string,string1_len);
 
var changed_string = new_part_a+part_b;
 
console.log(changed_string);
</script>


Output

GEEKSFOrneveropen

Time Complexity: O(N)
Auxiliary Space: O(N)

In these ways, you can solve these kinds of problems.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments