在C中将Char *转换为大写 [英] Converting Char * to Uppercase in C
问题描述
我正在尝试将c中的char *
转换为大写,但是功能toupper()
在这里不起作用.
I'm trying to convert a char *
to uppercase in c, but the function toupper()
doesn't work here.
我正在尝试获取temp值的名称,该名称是冒号之前的任何字符,在这种情况下为"Test",然后我想将其完全大写.
I'm trying to get the name of the the value of temp, the name being anything before the colon, in this case it's "Test", and then I want to capitalize the name fully.
void func(char * temp) {
// where temp is a char * containing the string "Test:Case1"
char * name;
name = strtok(temp,":");
//convert it to uppercase
name = toupper(name); //error here
}
我收到错误消息,该函数toupper期望一个int,但是接收到一个char *.问题是,我必须使用char *,因为这是函数要使用的((我不能在这里真正使用char数组,可以吗?).
I'm getting the error that the function toupper expects an int, but receives a char *. Thing is, I have to use char *'s, since that is what the function is taking in, (I can't really use char arrays here, can I?).
任何帮助将不胜感激.
推荐答案
toupper()
转换单个char
.
只需使用循环:
void func(char * temp) {
char * name;
name = strtok(temp,":");
// Convert to upper case
char *s = name;
while (*s) {
*s = toupper((unsigned char) *s);
s++;
}
}
详细信息:为所有unsigned char
和EOF
定义了标准库函数toupper(int)
.由于char
可能已签名,请转换为unsigned char
.
Detail: The standard Library function toupper(int)
is defined for all unsigned char
and EOF
. Since char
may be signed, convert to unsigned char
.
某些操作系统支持执行此操作的函数: upstr()
和 strupr()
Some OS's support a function call that does this: upstr()
and strupr()
这篇关于在C中将Char *转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!