在C ++中转换为大写 [英] Converting to uppercase in C++

查看:215
本文介绍了在C ++中转换为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有:

  const char * something =m 

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



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



我可以让 char * something =m; 包含M


一个字符串文字( char * something )。尝试数组:

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

要更改整个字符串:

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

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


Let's say you have:

const char * something = "m";

How would one make this uppercase, using toupper (or something else, if applicable)?

I want to use a char * instead of a string (I can use a string, but then I have to use str.c_str()).

So, how can I make char * something = "m"; contain "M"?

解决方案

I find you choice of C strings disturbing.. but anyway.

You can't change a string literal (char *something). Try an array:

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

To change an entire string:

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

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

这篇关于在C ++中转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆