【问题标题】:Converting to uppercase in C++在 C++ 中转换为大写
【发布时间】:2012-01-01 15:59:36
【问题描述】:

假设你有:

const char * something = "m";

如何使这个大写,使用 toupper(或其他,如果适用)?

我想使用char * 而不是string(我可以使用字符串,但必须使用str.c_str())。

那么,我怎样才能让char * something = "m"; 包含"M"

【问题讨论】:

  • 不,使用std::string。并选择一种语言:没有“C/C++”之类的东西。
  • 你想用char something[] = "\xEF\xAC\x83";做什么? (即ffi)

标签: c++ string uppercase toupper


【解决方案1】:

我发现你选择 C ​​字符串令人不安.. 但无论如何。

您不能更改字符串文字 (char *something)。尝试一个数组:

char something[] = "m";
something[0] = toupper(something[0]);

要更改整个字符串:

char something[] = "hello";
char *p = something;

while (*p) {
    *p = toupper(*p);
    p++;
}

【讨论】:

  • 它说:初始化程序无法确定“输入”的大小 const char * input = "m"; char something[] = 输入;某事[0] = toupper(某事[0]); char *p = 某事;
  • @John 这不是我说的:-) 再次阅读答案。更好的是,看看 Kerreks 的回答。
  • p 在这里无关紧要 - 仅使用 something 并且示例将更加清晰 IMO。
  • @ildjarn 但我不能增加它。我将不得不使用单独的迭代器。
  • @DietmarKühl 我知道。它通常是用宏实现的(我在 Plauger 的某个地方看到过),所以传递错误的东西是未定义的行为。如果您愿意,请编辑我的帖子,我会接受您的编辑。
【解决方案2】:

正如非常著名的 C 书 - Kernighan & Ritchie5.5 Character Pointers and Functions 部分中的The C Programming Language 中所解释的,

char amessage[] = "now is the time";    /* an array */
char *pmessage = "now is the time";     /* a pointer */

`amessage` is an array, just big enough to hold the 
sequence of characters and `'\0'` that initializes it. 
Individual characters within the array may be changed 
but `amessage` will always refer to the same storage. 
On the other hand, `pmessage` is a pointer, initialized 
to point to a string constant; the pointer may subsequently 
be modified to point elsewhere, but the result is undefined
if you try to modify the string contents.

OTOH,在C语言中,要转换成大写字母,可以参考下面的程序。

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int i=0;
    char str[]="Test String.\n";
    char c;

    while (str[i]) {
        c=str[i];
        putchar(toupper(c));
        i++;
    }

    return 0;
}

在 C++ 中

#include <iostream>
#include <string>
#include <locale>
using namespace std;

int main ()
{
    locale loc;

    string str="Test String.\n";

    for (size_t i=0; i<str.length(); ++i)
        cout << toupper(str[i],loc);

    return 0;
}

编辑:为 C 版本添加指针版本(根据@John 的要求)

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int i=0;
    char str[]="Test String.\n";
    char *ptr = str;

    while (*ptr) {
        putchar(toupper(*ptr));
        ptr++;  
    }   

    return 0;
}

希望对您有所帮助!

【讨论】:

  • @John 你能详细说明你的问题吗?!你的意思是,如何让一个指针指向一个数组?
  • @SangeethSaravanaraj,当您将 char 传递给 C toupper 和其他函数采用 int 中的字符时,必须首先将其转换为无符号字符,然后再转换为 int .
【解决方案3】:

对于原始数组,您可以使用与 std::string 相同的算法方法:

char s[] = "hello world";
std::transform(s, s + std::strlen(s), s, static_cast<int(*)(int)>(std::toupper));

出于显而易见的原因,您不能对不可变的字符串文字(如 const char * s = "hello world;")执行此操作,因此您不会为此进行额外的分配/复制。

更新: 正如 Ildjarn 在评论中所说,重要的是要注意字符串文字始终是只读的,即使出于历史原因您可以绑定它们指向可变的指针,例如char * s = "hello world";。如果你尝试这样做,任何体面的 C++ 编译器都会给你一记耳光,但它有效的 C++ —— 但实际上修改s 的任何元素的任何尝试都是未定义的行为。

【讨论】:

  • 我认为值得更深入地解释为什么char * s = "hello world;",没有const,也是不可变的——这是初学者的主要挂断。
  • @DietmarKühl:好点;您可能应该使用&lt;locale&gt; 附带的toupper 版本以获得更通用的解决方案。
  • @DietmarKühl:没有人能跟得上 zose 疯狂的德国人 :-) 现在他们确实有一个 capital SZ,但它不能用 char 来表示... 叹息
【解决方案4】:

您可以将 C-string 转换为 std::string,然后使用 boost::to_upper 来更改字符串或使用 boost::to_upper_copy 来创建字符串的大写副本。下面是代码示例:

#include <iostream>
#include <boost/algorithm/string/case_conv.hpp>

int main ()
{
  char const * s = "Test String.\n";
  std::string str(s);

  std::cout << boost::to_upper_copy(str).c_str() << std::endl;

  return 0;
}

希望这会有所帮助。

【讨论】:

    【解决方案5】:

    你可以这样做:

    #include <algorithm>
    #include <iterator>
    #include <ctype.h>
    
    char test[] = "m";
    
    std::transform(std::begin(test), std::end(test), std::begin(test), ::topper);
    

    这会将::toupper 函数应用于字符串的字符。这是来自 C 的全局命名空间中的 ::toupper 函数。std::toupper 有多个重载,::toupper 看起来比 static_cast&lt;int (*)(int)&gt;(&amp;std::toupper) 更优雅。

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 2016-05-12
      • 2016-01-10
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      相关资源
      最近更新 更多