【问题标题】:Converting Char * to Uppercase in C在 C 中将 Char * 转换为大写
【发布时间】:2016-05-12 22:29:24
【问题描述】:

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

我正在尝试获取 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

}

我得到的错误是函数 toupper 需要一个 int,但接收到一个 char *。问题是,我必须使用 char *,因为这是函数所接受的,(我不能在这里真正使用 char 数组,可以吗?)。

任何帮助将不胜感激。

【问题讨论】:

    标签: c string pointers strtok


    【解决方案1】:

    这个小功能怎么样?它假定 ASCII 表示的字符并就地修改字符串。

    void to_upper(char* string)
    {
        const char OFFSET = 'a' - 'A';
        while (*string)
        {
            *string = (*string >= 'a' && *string <= 'z') ? *string -= OFFSET : *string;
            string++;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      #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.
      }
      

      注意:如果您想改为小写字符串,请将@​​987654322@ 更改为tolower(s[a])

      【讨论】:

      • 很好奇,为什么使用类型unsigned long int s_lenunsigned s_len 甚至更好的size_t s_len?在a &lt; s_len 中使用unsigned long s_lenint a 似乎很奇怪。我希望变量是相同的类型。
      • 老实说,我是 C 和一般编程新手,所以很可能变量类型不是最好的。
      【解决方案3】:

      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++;
        }
      
      }
      

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

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

      【讨论】:

      • 哪些系统通常支持upstrupstrDebian manpage 表示它来自“Alliance CAD 系统”。
      【解决方案4】:

      toupper() 仅适用于单个字符。但是strupr() 是你想要的指向字符串的指针。

      【讨论】:

      • strupr 不是标准的。据我所知,它仅受 Microsoft 库的支持。
      • @interjay:Greenhills 和 Borland 也支持它。但你是对的,它不在 glibc 中。
      【解决方案5】:

      toupper() 一次作用于一个元素(int 参数,值范围与unsigned char 或 EOF 相同)。

      原型:

      int toupper(int c);

      您需要使用循环从您的字符串中一​​次提供一个元素。

      【讨论】:

      • 也许我会删除这个答案,因为它没有增加价值,但是DV的原因是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2021-11-30
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 2021-11-09
      • 2014-03-12
      相关资源
      最近更新 更多