在C++中转换为大写

Joh*_*ohn 1 c++ string uppercase toupper

假设你有:

const char * something = "m";
Run Code Online (Sandbox Code Playgroud)

如何使用toupper(或其他东西,如果适用)制作这个大写字母?

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

那么,如何才能让char * something = "m";含有"M"

cni*_*tar 6

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

您无法更改字符串文字(char *something).尝试一个数组:

char something[] = "m";
something[0] = toupper(something[0]);
Run Code Online (Sandbox Code Playgroud)

要更改整个字符串:

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

while (*p) {
    *p = toupper(*p);
    p++;
}
Run Code Online (Sandbox Code Playgroud)


Ker*_* SB 5

You can use the same algorithmic approach that you know for std::string for raw arrays:

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

You cannot do this for immutable string literals (like const char * s = "hello world;") for obvious reasons, so you won't get around an additional allocation/copy for that.

Update: As Ildjarn says in the comment, it's important to note that string literals are always read-only, even though for historical reasons you are allowed to bind them to a pointer-to-mutable, like char * s = "hello world";. Any decent C++ compiler should slap you in the face if you attempt this, but it is valid C++ -- but any attempt to actually modify any element of s is undefined behaviour.

  • 我认为可能值得更深入地解释为什么 `char * s = "hello world;"`,没有 `const`,也是不可变的——这是初学者的主要问题。 (3认同)

San*_*raj 5

As explained in the very famous C book - The C Programming Language by Kernighan & Ritchie in section 5.5 Character Pointers and Functions,

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.
Run Code Online (Sandbox Code Playgroud)

OTOH, in C, to convert to upper case letters, you can use the following program as a reference.

#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;
}
Run Code Online (Sandbox Code Playgroud)

In 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;
}
Run Code Online (Sandbox Code Playgroud)

EDIT: Adding pointer version (as requested by @John) for the C version

#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;
}
Run Code Online (Sandbox Code Playgroud)

Hope it helps!