c语言toupper
In this article, we’ll take a look at how we can use the toupper() function in C.
在本文中,我们将研究如何在C中使用toupper()函数。
This is a very straightforward function, which converts a character to upper case. Let’s quickly look at some examples of this function.
这是一个非常简单的函数,它将字符转换为大写。 让我们快速看一下该函数的一些示例。
C语言中toupper()的基本语法 (Basic Syntax of toupper() in C)
This function, similar to the isdigit() function, is present in the <ctype.h>
header file, so we must first include this file.
该函数类似于isdigit()函数 ,存在于<ctype.h>
头文件中,因此我们必须首先包含此文件。
This function takes in a single character as argument, and returns an integer.
此函数接受单个字符作为参数,并返回一个整数。
#include <ctype.h>
int toupper(int ch);
Here, notice that the argument type is an int
. This is because characters can be type-cast to integers for the function to be able to work on the ASCII values.
在这里,请注意,参数类型为int
。 这是因为可以将字符类型转换为整数,以便该函数能够使用ASCII值。
If the character is a lower case letter, like ‘a’, ‘b’, the toupper() function will return convert this to the corresponding upper case letter, and return that ASCII value.
如果字符是小写字母,例如“ a”,“ b”,则toupper()函数将返回该值并将其转换为相应的大写字母,并返回该ASCII值。
Otherwise, it will simply return the same upper-case string. Observe that this could happen if the input parameter could not be converted to upper case!
否则,它将仅返回相同的大写字符串。 请注意,如果输入参数无法转换为大写字母,可能会发生这种情况!
Now, let’s look at a simple example in C.
现在,让我们来看一个简单的C语言示例。
使用toupper()函数–一个示例 (Using the toupper() function – An example)
The below program will take a string as input, and will convert all the lower-cased letters to uppercase
下面的程序将字符串作为输入,并将所有小写字母转换为大写字母
For example, if we pass the string “JOUrnalDev” as input, we’ll get the output as “JOURNALDEV”, which only consists of upper-case letters.
例如,如果传递字符串“ JOUrnalDev”作为输入,则输出将为“ JOURNALDEV”,其中仅包含大写字母。
Notice that this also includes all the upper case characters in the original string!
请注意,这还包括原始字符串中的所有大写字符!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char input[] = "JOUrnalDev";
char output[256];
// Get the input size
int size = strlen(input);
for (int i=0; i<size; i++)
// Store the upper case letters to output[i]
output[i] = toupper(input[i]);
printf("Input: %s\n", input);
printf("Output: %s", output);
return 0;
}
Output
输出量
Input: JOUrnalDev
Output: JOURNALDEV
Indeed, as you can see, the output string only consists of upper-case letters!
确实,如您所见,输出字符串仅包含大写字母!
某些输入的不确定行为 (Undefined Behavior with certain inputs)
While the toupper() function tries it’s best to convert a value to upper-case, sometimes, it may not be possible.
当toupper()函数尝试进行转换时,最好将一个值转换为大写字母,但有时是不可能的。
According to the Linux standard, the behavior is undefined if the value is beyond the limits of an unsigned character value (256), or if the value is EOF
.
根据Linux标准,如果该值超出无符号字符值 (256)的限制,或者该值是EOF
,则该行为是不确定的。
// Undefined behavior!
printf("%c\n", toupper(EOF));
pritnf("%c\n", toupper(1234));
Due to this, always be careful and place a check on your input, before passing it to toupper()
!
因此,在将输入传递给toupper()
之前,请务必小心并检查输入内容!
结论 (Conclusion)
In this article, we saw how we could use the toupper() function in C to convert a character to upper-case.
在本文中,我们看到了如何在C语言中使用toupper()函数将字符转换为大写字母。
参考资料 (References)
- Linux manual page on using the toupper() function in C 有关在C语言中使用toupper()函数的Linux手册页
c语言toupper