📜  小写到大写——一个有趣的事实

📅  最后修改于: 2022-05-13 01:57:07.121000             🧑  作者: Mango

小写到大写——一个有趣的事实

问题:给定一个仅包含小写字母的字符串,生成一个具有相同字母但大写的字符串。

Input : GeeksForGeeks
Output : GEEKSFORGEEKS 

我们想到的第一个方法是

C++
// C++ program to convert a string to uppercase
#include 
using namespace std;
 
// Converts a string to uppercase
string to_upper(string &in)
{
    for (int i = 0; i < in.length(); i++)
          if ('a' <= in[i] <= 'z')
              in[i] = in[i] - 'a' + 'A';
    return in;
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   cout << to_upper(str);
   return 0;
}


Java
// Java program to convert a string to uppercase
 
class GFG
{
 
    // Converts a string to uppercase
    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);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        System.out.println(to_upper(str.toCharArray()));
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to convert a string to uppercase
 
# Converts a string to uppercase
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;
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks";
    print(to_upper(str));
 
# This code is contributed by Rajput-Ji


C#
// C# program to convert a string to uppercase
using System;
 
class GFG
{
    // Converts a string to uppercase
    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);
    }
 
    // Driver code
    public static void Main()
    {
        String str = "geeksforgeeks";
        Console.WriteLine(to_upper(str.ToCharArray()));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to convert a string to uppercase
#include 
using namespace std;
 
// Converts a string to uppercase
string to_upper(string &in)
{
    for (int i = 0; i < in.length(); i++)
          if ('a' <= in[i] <= 'z')
              in[i] &= ~(1 << 5);
    return in;
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   cout << to_upper(str);
   return 0;
}


Java
// Java program to convert a string to uppercase
class GFG
{
 
// Converts a string to uppercase
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);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.println(to_upper(str.toCharArray()));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to convert
# a string to uppercase
 
# Converts a string to uppercase
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;
 
# Driver code
if __name__ == '__main__':
    string = "geeksforgeeks";
    print(to_upper(string));
 
# This code is contributed by PrinciRaj1992


C#
// C# program to convert a string to uppercase
using System;
 
class GFG
{
 
// Converts a string to uppercase
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);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.WriteLine(to_upper(str.ToCharArray()));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出 :

GEEKSFORGEEKS

另一方面,更有趣的解决方案是:

C++

// C++ program to convert a string to uppercase
#include 
using namespace std;
 
// Converts a string to uppercase
string to_upper(string &in)
{
    for (int i = 0; i < in.length(); i++)
          if ('a' <= in[i] <= 'z')
              in[i] &= ~(1 << 5);
    return in;
}
 
// Driver code
int main()
{
   string str = "geeksforgeeks";
   cout << to_upper(str);
   return 0;
}

Java

// Java program to convert a string to uppercase
class GFG
{
 
// Converts a string to uppercase
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);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.println(to_upper(str.toCharArray()));
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program to convert
# a string to uppercase
 
# Converts a string to uppercase
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;
 
# Driver code
if __name__ == '__main__':
    string = "geeksforgeeks";
    print(to_upper(string));
 
# This code is contributed by PrinciRaj1992

C#

// C# program to convert a string to uppercase
using System;
 
class GFG
{
 
// Converts a string to uppercase
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);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    Console.WriteLine(to_upper(str.ToCharArray()));
}
}
 
// This code is contributed by PrinciRaj1992

Javascript


输出 :

GEEKSFORGEEKS

说明: ASCII 表的构造方式使得小写字母的二进制表示与大写字母的二进制表示几乎相同。唯一的区别是第六位,只为小写字母设置。这个优雅的函数所做的是取消设置 in[i] 的索引 5 的位,因此,使该字符小写。

缺点:该策略仅适用于字母字符。如果输入包含数字或标点符号,我们将不得不使用幼稚的方式。

示例:字符'A' 是整数 65 = (0100 0001)2,而字符'a' 是整数 = 97 = (0110 0001) 2 。 (注意 97 – 65 = 32。你能猜到为什么吗?)

练习:

  1. 编写一个函数,将字符串的所有字母变为小写。示例:GeeksForGeeks 变成了 geeksforgeeks。
  2. 编写一个改变字符串大小写的函数。示例:GeeksForGeeks 变成 gEEKSfORgEEKS。