Lower case to upper case – An interesting fact
Last Updated :
24 Mar, 2023
Problem: Given a string containing only lowercase letters, generate a string with the same letters, but in uppercase.
Input : GeeksForGeeks
Output : GEEKSFORGEEKS
The first method that comes to our mind is
C++
#include <iostream>
using namespace std;
string to_upper(string ∈)
{
for (int i = 0; i < in.length(); i++)
if ('a' <= in[i] <= 'z')
in[i] = in[i] - 'a' + 'A';
return in;
}
int main()
{
string str = "geeksforgeeks";
cout << to_upper(str);
return 0;
}
|
Java
class GFG
{
static String to_upper(char[] in)
{
for (int i = 0; i < in.length; i++)
{
if ('a' <= in[i] & in[i] <= 'z')
{
in[i] = (char) (in[i] - 'a' + 'A');
}
}
return String.valueOf(in);
}
public static void main(String[] args)
{
String str = "geeksforgeeks";
System.out.println(to_upper(str.toCharArray()));
}
}
|
Python3
def to_upper(string):
for i in range(len(string)):
if ('a' <= string[i] <= 'z'):
string = (string[0:i] + chr(ord(string[i]) -
ord('a') + ord('A')) +
string[i + 1:])
return string;
if __name__ == '__main__':
str = "geeksforgeeks";
print(to_upper(str));
|
C#
using System;
class GFG
{
static String to_upper(char []In)
{
for (int i = 0; i < In.Length; i++)
{
if ('a' <= In[i] & In[i] <= 'z')
{
In[i] = (char) (In[i] - 'a' + 'A');
}
}
return String.Join("", In);
}
public static void Main()
{
String str = "geeksforgeeks";
Console.WriteLine(to_upper(str.ToCharArray()));
}
}
|
Javascript
<script>
function to_upper(input)
{
var result = new Array(input.length);
for (var i = 0; i < input.length; i++)
if ("a".charCodeAt(0) <= input[i].charCodeAt(0) <= "z".charCodeAt(0))
result[i] = String.fromCharCode(
input[i].charCodeAt(0) - "a".charCodeAt(0) + "A".charCodeAt(0)
);
return result.join("").toString();
}
var str = "geeksforgeeks";
document.write(to_upper(str));
</script>
|
Time Complexity: O(n),where n is length of string.
Auxiliary Space: O(1)
A more interesting solution, on the other hand, would be:
C++
#include <iostream>
using namespace std;
string to_upper(string ∈)
{
for (int i = 0; i < in.length(); i++)
if ('a' <= in[i] <= 'z')
in[i] &= ~(1 << 5);
return in;
}
int main()
{
string str = "geeksforgeeks";
cout << to_upper(str);
return 0;
}
|
Java
class GFG
{
static String to_upper(char[] in)
{
for (int i = 0; i < in.length; i++)
if ('a' <= in[i] && in[i] <= 'z')
in[i] &= ~(1 << 5);
return String.valueOf(in);
}
public static void main(String[] args)
{
String str = "geeksforgeeks";
System.out.println(to_upper(str.toCharArray()));
}
}
|
Python3
def to_upper(s):
for i in range(len(s)):
if ('a' <= s[i] <= 'z'):
s = s[0:i] + chr(ord(s[i]) & \
(~(1 << 5))) + s[i + 1:];
return s;
if __name__ == '__main__':
string = "geeksforgeeks";
print(to_upper(string));
|
C#
using System;
class GFG
{
static String to_upper(char[] str)
{
for (int i = 0; i < str.Length; i++)
if ('a' <= str[i] && str[i] <= 'z')
str[i] = (char)((int)str[i]&(~(1 << 5)));
return String.Join("", str);
}
public static void Main(String[] args)
{
String str = "geeksforgeeks";
Console.WriteLine(to_upper(str.ToCharArray()));
}
}
|
Javascript
<script>
function to_upper(In)
{
let n = In.length;
for(let i = 0; i < In.length; i++)
if ('a' <= In[i] && In[i] <= 'z')
In[i] = String.fromCharCode(
In[i].charCodeAt(0) & (~(1 << 5)));
return (In).join("");
}
let str = "geeksforgeeks";
document.write(to_upper(str.split("")));
</script>
|
Time Complexity: O(n),where n is length of string.
Auxiliary Space: O(1)
Method 3(using C++ STL)
In C++ STL there is a function called “transform” which converts the case of all latter of a string.
Below is an implementation of that function:
C++
#include<bits/stdc++.h>
using namespace std;
void transform_to_upper(string &s;){
transform(s.begin(),s.end(),s.begin(),::toupper);
cout<<"string with uppercase latter is: "<<endl;
cout<<s;
}
int main() {
string s="GeeksForGeeks";
transform_to_upper(s);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void transformToUpper(StringBuilder s)
{
s = new StringBuilder(s.toString().toUpperCase());
System.out.println("String with uppercase letters is:");
System.out.println(s);
}
public static void main(String[] args) {
StringBuilder s = new StringBuilder("GeeksForGeeks");
transformToUpper(s);
}
}
|
Python3
def transform_to_upper(s):
s = s.upper()
print("String with uppercase letters is: ")
print(s)
if __name__ == '__main__':
s = "GeeksForGeeks"
transform_to_upper(s)
|
C#
using System;
using System.Text;
public class MainClass {
public static void TransformToUpper(StringBuilder s)
{
s = new StringBuilder(s.ToString().ToUpper());
Console.WriteLine("String with uppercase letters is:");
Console.WriteLine(s);
}
public static void Main(string[] args) {
StringBuilder s = new StringBuilder("GeeksForGeeks");
TransformToUpper(s);
}
}
|
Javascript
function transformToUpperCase(s) {
s = s.toUpperCase();
console.log("String with uppercase letters is: ");
console.log(s);
}
const s = "GeeksForGeeks";
transformToUpperCase(s);
|
Output
string with uppercase latter is:
GEEKSFORGEEKS
Time Complexity: O(n),where n is length of string.
Auxiliary Space: O(1)
Explanation: The ASCII table is constructed in such way that the binary representation of lowercase letters is almost identical of binary representation of uppercase letters. The only difference is the sixth bit, setted only for lowercase letters. What that elegant function does is unset the bit of index 5 of in[i], consequently, making that character lowercase.
Disadvantages: That strategy works only for alphabetical characters. If the input contains numbers or punctuations, we’ll have to use the naive way.
Example: Character ‘A’ is integer 65 = (0100 0001)2, while character ‘a’ is integer = 97 = (0110 0001)2. (Note that 97 – 65 = 32. Can you guess why?)
Exercises:
- Write a function to make all letters of a string lowercase. Example: GeeksForGeeks turns geeksforgeeks.
- Write a function that change the case of a string. Example: GeeksForGeeks turns gEEKSfORgEEKS.
Similar Reads
Convert characters of a string to opposite case
Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput : hello every oneOutput : HELLO EVERY ONEASCII values of alphabets: A - Z = 65
15+ min read
PHP Change strings in an array to uppercase
Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized. Examples: Input : arr[] = ("geeks", "For", "GEE
3 min read
Convert vowels into upper case character in a given string
Given string str, the task is to convert all the vowels present in the given string to uppercase characters in the given string. Examples: Input: str = âGeeksforGeeksâOutput: GEEksfOrGEEksExplanation:Vowels present in the given string are: {'e', 'o'}Replace 'e' to 'E' and 'o' to 'O'.Therefore, the r
7 min read
Check if lowercase and uppercase characters are in same order
Given string str containing only lower and uppercase alphabets. The task is to check if both lowercase characters and uppercase characters follow the same order respectively. Note: If a character occurs more than once in lowercase then the occurrence of the same character in the uppercase should be
4 min read
Program to toggle all characters in a string
Given a string, the task is to toggle all the characters of the string i.e to convert Upper case to Lower case and vice versa. Examples: Input : gfg Output : GFG Input : aBc12# Output : AbC12# Input : tu@kmiNi Output : TU@KMInI Traverse the given string, if uppercase characters come, convert into lo
5 min read
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 = "geeksforgeeks";string1[0] = "G";console.log(string1);As strings are immutable, so we cannot change the character of the string,
9 min read
Count of camel case characters present in a given string
Given a string S, the task is to count the number of camel case characters present in the given string. The camel case character is defined as the number of uppercase characters in the given string. Examples: Input: S = "ckjkUUYII"Output: 5Explanation: Camel case characters present are U, U, Y, I an
7 min read
Check whether the given character is in upper case, lower case or non alphabetic character
Given a character, the task is to check whether the given character is in upper case, lower case, or non-alphabetic character Examples: Input: ch = 'A'Output: A is an UpperCase characterInput: ch = 'a'Output: a is an LowerCase characterInput: ch = '0'Output: 0 is not an alphabetic characterApproach:
11 min read
isupper() and islower() and their application in C++
In C++, isupper() and islower() are predefined functions used for string and character handling. cstring.h is the header file required for string functions and cctype.h is the headerfile required for character functions. isupper() Function: This function is used to check if the argument contains any
2 min read
Toggle case of a string using Bitwise Operators
Given a string, write a function that returns toggle case of a string using the bitwise operators in place.In ASCII codes, character âAâ is integer 65 = (0100 0001)2, while character âaâ is integer 97 = (0110 0001)2. Similarly, character 'D' is integer 68 = (0100 0100)2, while character 'd' is integ
4 min read