100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 字符串大写字符串转小写js_C ++字符串大写和小写

字符串大写字符串转小写js_C ++字符串大写和小写

时间:2021-01-09 14:29:57

相关推荐

字符串大写字符串转小写js_C ++字符串大写和小写

字符串大写字符串转小写js

In this article, we will dive into theconversion of the input string to Lowercase and Uppercase in C++.C++ String classprovides 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-intoupper()function to convert the input String toUppercase.

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, thecstringpackage contains the String related functions. Further,strlen()function is used to calculate the length of the input string.

在上面的代码片段中,cstring包包含与String相关的函数。 此外,strlen()函数用于计算输入字符串的长度。

Theputchar()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 theASCII valuesof 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 of32 i.e. 97-65between the range ofASCII valuesof lowercase and uppercase alphabets.

如上所示,小写和大写字母的ASCII值范围之间恰好相差32,即97-65

So in order to convert the input to uppercase, we need tosubtract 32from the ASCII value of the input character.

因此,为了将输入转换为大写,我们需要从输入字符的ASCII值中减去32

Output:

输出:

Enter a character:fConverted character to UPPERCASE:F

C ++字符串转换为小写 (C++ String to Lowercase)

C++ String has got built-intolower()function to convert the input string tolowercase.

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 toadd 32to the ASCII value of the input character to convert it to lowercase.

我们需要在输入字符的ASCII值上加上32,以将其转换为小写。

Output:

输出:

Enter a character:RConverted 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)

C++ String to UppercaseC ++字符串转换为大写C++ String to LowerCaseC ++字符串转换成小写

翻译自: /36852/string-uppercase-lowercase-c-plus-plus

字符串大写字符串转小写js

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。