C语言 在 C 中将 Char * 转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35181913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:19:48  来源:igfitidea点击:

Converting Char * to Uppercase in C

cstringpointersstrtok

提问by EDEDE

I'm trying to convert a char *to uppercase in c, but the function toupper()doesn't work here.

我正在尝试将char *c 中的 a转换为大写,但该函数toupper()在这里不起作用。

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.

我正在尝试获取 temp 值的名称,名称是冒号之前的任何名称,在本例中为“Test”,然后我想将名称完全大写。

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

}

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 需要一个 int,但收到一个 char *。问题是,我必须使用 char *'s,因为这是函数所接受的,(我不能在这里真正使用 char 数组,是吗?)。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by chux - Reinstate Monica

toupper()converts a single char.

toupper()转换单个char.

Simply use a loop:

只需使用循环:

void func(char * temp) {
  char * name;
  name = strtok(temp,":");

  // Convert to upper case
  char *s = name;
  while (*s) {
    *s = toupper((unsigned char) *s);
    s++;
  }

}

Detail: The standard Library function toupper(int)is defined for all unsigned charand EOF. Since charmay be signed, convert to unsigned char.

详细信息:标准库函数toupper(int)是为所有unsigned char和定义的EOF。由于char可能已签名,因此转换为unsigned char.

Some OS's support a function call that does this: upstr()and strupr()

某些操作系统支持执行此操作的函数调用:upstr()strupr()

回答by wallyk

toupper()works only on a single character. But there is strupr()which is what you want for a pointer to a string.

toupper()仅适用于单个字符。但是strupr()对于指向字符串的指针,您需要的是什么。

回答by Sourav Ghosh

toupper()works on one element (intargument, value ranging the same as of unsigned charor EOF) at a time.

toupper()一次处理一个元素(int参数,值范围与 ofunsigned char或 EOF相同)。

Prototype:

原型:

int toupper(int c);

int toupper(int c);

You need to use a loop to supply one element at a time from your string.

您需要使用循环从string一次提供一个元素。

回答by Mike

For those of you who want to uppercase a string and store itin a variable (that was what I was looking for when I read these answers).

对于那些想要大写字符串并将其存储在变量中的人(这就是我阅读这些答案时所寻找的)。

#include <stdio.h>  //<-- You need this to use printf.
#include <string.h>  //<-- You need this to use string and strlen() function.
#include <ctype.h>  //<-- You need this to use toupper() function.

int main(void)
{
    string s = "I want to cast this";  //<-- Or you can ask to the user for a string.

    unsigned long int s_len = strlen(s); //<-- getting the length of 's'.  

    //Defining an array of the same length as 's' to, temporarily, store the case change.
    char s_up[s_len]; 

    // Iterate over the source string (i.e. s) and cast the case changing.
    for (int a = 0; a < s_len; a++)
    {
        // Storing the change: Use the temp array while casting to uppercase.  
        s_up[a] = toupper(s[a]); 
    }

    // Assign the new array to your first variable name if you want to use the same as at the beginning
    s = s_up;

    printf("%s \n", s_up);  //<-- If you want to see the change made.
}

Note:If you want to lowercase a string instead, change toupper(s[a])to tolower(s[a]).

注意:如果要改为小写字符串,请更改toupper(s[a])tolower(s[a]).