字符串大写字符串转小写js
In this article, we will dive into the conversion of the input string to Lowercase and Uppercase in C++. C++ String class provides a huge number of built-in functions to perform operations over the input String.
在本文中,我们将深入探讨C ++中输入字符串到小写和大写的转换 。 C ++ String类提供了大量内置函数来对输入String进行操作。
C ++字符串转换为大写 (C++ String to Uppercase)
C++ String has got built-in toupper()
function to convert the input String to Uppercase.
C ++ String具有内置的toupper()
函数,可将输入的String转换为Uppercase 。
Syntax:
句法:
toupper(input_string)
Example:
例:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char arr[] = "Engineering Discipline.";
cout << "Original String:\n"<< arr<< endl;
cout<<"String in UPPERCASE:\n";
for (int x=0; x<strlen(arr); x++)
putchar(toupper(arr[x]));
return 0;
}
In the above snippet of code, the cstring package contains the String related functions. Further, strlen() function is used to calculate the length of the input string.
在上面的代码片段中, cstring包包含与String相关的函数。 此外, strlen()函数用于计算输入字符串的长度。
The putchar() method is used to display the data on to the screen/console.
putchar()方法用于将数据显示在屏幕/控制台上。
Output:
输出:
Original String:
Engineering Discipline.
String in UPPERCASE:
ENGINEERING DISCIPLINE.
将输入字符转换为大写 (Converting an input character to Uppercase)
We can even convert the characters/string to Uppercase/Lowercase considering the ASCII values of the input characters.
考虑到输入字符的ASCII值 ,我们甚至可以将字符/字符串转换为大写/小写。
ASCII values for lower case alphabets (a-z):97 – 122
小写字母(az)的ASCII值: 97 – 122
ASCII values for upper case alphabets (A-Z):65 – 92
大写字母(AZ)的ASCII值: 65 – 92
Example:
例:
#include <iostream>
using namespace std;
int main()
{
char X;
cout<<"Enter a character:";
cin>>X;
X=X-32;
cout<<"Converted character to UPPERCASE:";
cout<<X;
return 0;
}
As seen above, there happens to be a difference of 32 i.e. 97-65 between the range of ASCII values of lowercase and uppercase alphabets.
如上所示,小写和大写字母的ASCII值范围之间恰好相差32,即97-65 。
So in order to convert the input to uppercase, we need to subtract 32 from the ASCII value of the input character.
因此,为了将输入转换为大写,我们需要从输入字符的ASCII值中减去32 。
Output:
输出:
Enter a character:f
Converted character to UPPERCASE:F
C ++字符串转换为小写 (C++ String to Lowercase)
C++ String has got built-in tolower()
function to convert the input string to lowercase.
C ++ String具有内置的tolower()
函数,可将输入的字符串转换为小写 。
Syntax:
句法:
tolower(input)
Example:
例:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char arr[] = "Engineering Discipline.";
cout << "Original String:\n"<< arr<< endl;
cout<<"String in lowercase:\n";
for (int x=0; x<strlen(arr); x++)
putchar(tolower(arr[x]));
return 0;
}
Output:
输出:
Original String:
Engineering Discipline.
String in lowercase:
engineering discipline.
将输入字符转换为小写 (Converting an input character to Lowercase )
Example:
例:
#include <iostream>
using namespace std;
int main()
{
char X;
cout<<"Enter a character:";
cin>>X;
X=X+32;
cout<<"Converted character to lowercase:";
cout<<X;
return 0;
}
We need to add 32 to the ASCII value of the input character to convert it to lowercase.
我们需要在输入字符的ASCII值上加上32 ,以将其转换为小写。
Output:
输出:
Enter a character:R
Converted character to lowercase:r
结论 (Conclusion)
In this article, we have understood the conversion of character and String input to Lowercase and Uppercase in C++. The important thing to note with the ASCII methods is the fact that they’re simply converting the entered characters to ASCII and then back. If someone enters a number instead of a character, you’ll get a random output.
在本文中,我们了解了在C ++中将字符和字符串输入转换为小写和大写的情况。 ASCII方法要注意的重要一点是,它们只是将输入的字符转换为ASCII,然后又转换回ASCII。 如果有人输入数字而不是字符,您将获得随机输出。
So you can either handle the inputs and make sure that the entered values are actually characters, or simply use the toupper() and tolower() functions. We hope this tutorial has been useful to you. Comment below if you have any questions.
因此,您可以处理输入并确保输入的值实际上是字符,或者只使用toupper()和tolower()函数。 我们希望本教程对您有所帮助。 如果您有任何疑问,请在下面评论。
参考资料 (References)
翻译自: https://www.journaldev.com/36852/string-uppercase-lowercase-c-plus-plus
字符串大写字符串转小写js