2012-01-01 86 views
1

比方说,你有:(或别的东西,如果适用)转换为大写的C++

const char * something = "m"; 

如何将一个使这个大写字母,使用TOUPPER?

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

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

+5

不,请使用'std :: string'。并选择一种语言:没有“C/C++”这样的东西。 – 2012-01-01 16:03:52

+0

你想用'char something [] =“\ xEF \ xAC \ x83”做什么?''? (即ffi) – ybungalobill 2012-01-01 16:04:19

回答

6

我找到你选择C字符串的干扰..但无论如何。

你不能改变一个字符串(char *something)。尝试一个数组:

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

要改变整个字符串:

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

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

它说:initializer无法确定'input'的大小const char * input =“m”; char something [] =输入; 东西[0] = toupper(东西[0]); char * p = something; – John 2012-01-01 16:18:08

+0

@John这不是我说的:-)再次阅读答案。更好的是,看看Kerreks的回答。 – cnicutar 2012-01-01 16:19:42

+0

'p'在这里是无关紧要的 - 仅仅使用'something',这个例子会更清晰IMO。 – ildjarn 2012-01-01 16:27:58

4

您可以使用您知道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的任何元素都是未定义的行为。在第5.5 Character Pointers and FunctionsThe C Programming Language通过Kernighan & Ritchie

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,转换为大写字母,您可以使用下面的程序作为参考 -

+3

我认为可能值得更深入地解释为什么'char * s =“hello world;”',sans'const'也是不可变的 - 这是初学者的主要困扰。 – ildjarn 2012-01-01 16:26:35

+0

@DietmarKühl:好点;你应该使用''附带的'toupper'版本来获得更通用的解决方案。 – 2012-01-01 17:31:52

+0

@DietmarKühl:没有人能跟上zose疯狂的德国人:-)现在他们*有一个[大写SZ](http://en.wikipedia.org/wiki/Capital_ß),但它不能用char '...... *叹* – 2012-01-01 17:49:03

4

正如在非常有名的本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版

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

希望它能帮助!

+0

你怎么能在数组里面有指针? – John 2012-01-01 16:24:40

+0

@约翰你能否详细说明你的问题?你的意思是,如何让指针指向数组? – 2012-01-01 16:26:13

+0

@SangeethSaravanaraj,当你传递一个'char'给C toupper和其他函数接受一个'int'中的字符时,它必须首先被转换为一个无符号字符,然后转换为一个'int'。 – AProgrammer 2012-01-01 16:36:22

1

您可以将C-字符串的std :: string,然后使用boost :: to_upper改变串到位或升压:: 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; 
} 

希望这会有所帮助。

0

你可以这样做:

#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<int (*)(int)>(&std::toupper)更优雅。