我有指针数组,我想要lowerCase,我该怎么做?
char *myPointer = "HELLO MY NAME IS POINTER";
for(....)
myPointer[i] = tolower(myPointer[i]);
我的应用程序崩溃了。 我需要使用指针!
谢谢
答案 0 :(得分:4)
myPointer
指向字符串文字,修改它为undefined behaviour。如果您想要修改它,可以使用数组:
char myPointer[]= "HELLO MY NAME IS POINTER";
答案 1 :(得分:1)
查看tolower,它返回给定字符的小写版本。
char* myPointer = "OH MY HOST";
const int length = strlen( myPointer ); // get the length of the text
char* lower = ( char* )malloc( length + 1 ); // allocate 'length' bytes + 1 (for null terminator) and cast to char*
lower[ length ] = 0; // set the last byte to a null terminator
// copy all character bytes to the new buffer using tolower
for( int i = 0; i < length; i++ )
{
lower[ i ] = tolower( myPointer[ i ] );
}