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


To convert strings into lowercase and uppercase, we can use the JavaScript string class’s built-in methods like toLowerCase() and toUpperCase(). Furthermore, we can use the string length property or substring() method to split the string in half part.

We will learn two approaches in this tutorial to convert half string to uppercase and the other half to lowercase using JavaScript.

Use the for-loop, toUpperCase(), and toLowerCase() method

We can use the for-loop to get the half-string. After that, we can use the toUpperCase() method to convert the first half of the string the uppercase. After that, we need to use the toLowerCase() method to convert the second half of the string into lowercase.

Syntax

Users can follow the syntax below to use the toUpperCase() and toLowerCase() methods to convert the first half of the string into uppercase and another half into lowercase.

for (let k = 0; k < length / 2; k++) {
   newStr += string[k].toUpperCase();
}
for (k = length / 2; k < length; k++) {
   newStr += string[k].toLowerCase();
}

In the above syntax, the length is a string length, and we convert the particular character of the string into upper and lower case.

Algorithm

  • Step 1 − Use the length property of the string and get the string length.

  • Step 2 − Create a newStr variable and initialize it with the empty string to store the newly transformed string.

  • Step 3 − Use the for-loop to iterate through the first half. Also, convert every character of the first half in uppercase and append it to the newStr string variable.

  • Step 4 − Now, iterate through another half of the string using the for loop, and convert every character to lowercase using the toLowerCase() method. Also, append a lowercase character to the newStr variable.

  • Step 5 − The newStr variable contains the transformed string.

Example 1

In the example below, we have created the string containing some characters. After that, we used the above algorithm to convert the first half of the string into uppercase and another half into lowercase.

In the output, users can observe the transformed string.

<html>
<body>
   <h2>Using the <i> for-loop, toUpperCase(), and toLowerCase() </i> methods </h2>
   <h3>Converting first half of the string in uppercase and another half in lowercase</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let string = "fdSDksndsMSDSAXS";
      let length = string.length;
      let newStr = "";
      for (let k = 0; k < length / 2; k++) {
         newStr += string[k].toUpperCase();
      }

      for (k = length / 2; k < length; k++) {
         newStr += string[k].toLowerCase();
      }

      output.innerHTML += "The original string is " + string + "<br/>";
      output.innerHTML += "The converted string is " + newStr + "<br/>";
   </script>
</body>
</html>

Steps

Step 1 − Get the length of the string in the length variable.

Step 2 − Create a firstHalf, and secondHalf variable and initialize them with the empty string.

Step 3 − Now, use the for-of loop to iterate through every string character. Also, in every iteration, append a character to the firstHalf string variable and check the value of the k variable. If it is greater than length/2, break the loop.

Step 4 − Now, use the for-of loop again to iterate through the second half and perform the same operation we have done for the first half.

Step 5 − Next, convert the firstHalf to uppercase and secondHalf to lowercase. After that, merge both strings using the ‘+’ operator.

Example 2

In the example below, the first for-of loop finds the firstHalf of the string, and another for-of loop finds the secondHalf. We have also shown the firstHalf and secondHalf variable values in the output with the transformed string.

<html>
<body>
   <h2>Using the <i>for-of loop, toUpperCase(), and toLowerCase()</i> methods to convert first half of the string in uppercase and another half in lowercase.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let originalString = "fsfJKkjkUYFERSDFXwaZAsawecas";
      let length = originalString.length;
      let firstHalf = "";
      let secondHalf = "";
      let k = 0;
      for (let char of originalString) {
         if (k < length / 2) {
            firstHalf += char
            k++;
         } else {
            break;
         }
      }
      k=0;
      for (let char of originalString) {
         if (k>= length/2 && k < length) {
            secondHalf += char      
         } 
         k++;
      }
      let convertedStr = firstHalf.toUpperCase() + secondHalf.toLowerCase();
      output.innerHTML += "The original string is " + originalString + "<br/>";
      output.innerHTML += "The first half of the string is " + firstHalf + "<br/>";
      output.innerHTML += "The second half of the string is " + secondHalf + "<br/>";
      output.innerHTML += "The converted string is " + convertedStr + "<br/>";
   </script>
</body>
</html>

Use the substr(), toUpperCase(), and toLowerCase() method

The substr() method lets us get the substring from the original string. In this approach, we will get the first half substring and the second half substring using the substr() method.

Syntax

Users can follow the syntax below to use the substr(), toUpperCase(), and toLowerCase() methods to convert the first half to uppercase and another half to lowercase.

let convertedStr = stringToConvert.substr(0, length / 2).toUpperCase() + stringToConvert.substr(length / 2).toLowerCase();

In the above syntax, we used the substr() method two times to get the first half string and another half string.

Algorithm

Step 1 − Get the length of the string.

Step 2 − Use the substr() method to get the first half. Users need to pass 0 as the first parameter representing the starting point and length/2 as the second parameter representing the length of the substring.

Step 3 − Use the substr() method again to get another half. Pass the length/2 as a first parameter to get the substring from length/2 to end.

Example 3

In the example below, we have taken two halves of the substring using the substr() method and used the toUpperCase() method with the first half and toLowerCase() method with the second half.

<html>
<body>
   <h2>Using the <i> substr(), toUpperCase(), and toLowerCase() </i> methods to convert first half of the string in uppercase and another half in lowercase.</h2>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      let stringToConvert = "erteDCSRDEWdEREre4ere";
      let length = stringToConvert.length;
      let convertedStr = stringToConvert.substr(0, length / 2).toUpperCase() + stringToConvert.substr(length / 2).toLowerCase();

      output.innerHTML += "The original string is " + stringToConvert + "<br/>";
      output.innerHTML += "The converted string is " + convertedStr + "<br/>";
   </script>
</body>
</html>

Users learned to convert the first half of the string to uppercase and another half to lowercase using the for loop and substr() method. Using the substr() method, we need to write a single line of code to achieve our goal. So, it's recommended approach.

Updated on: 16-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements