C 如何将指向字符数组的带下划线的指针转换为大小写?
我正在尝试编写一个函数,将“word_string”转换为“wordString”。但是,下面代码的输出是“wordSttring”。在用下一个元素的大写字母替换未得分后,我无法跳到数组的下一个元素。有什么建议吗C 如何将指向字符数组的带下划线的指针转换为大小写?,c,C,我正在尝试编写一个函数,将“word_string”转换为“wordString”。但是,下面代码的输出是“wordSttring”。在用下一个元素的大写字母替换未得分后,我无法跳到数组的下一个元素。有什么建议吗 void convert_to_camel(字符*短语){ int j=0; 对于(int i=0;i您没有正确地处理删除\uu的操作,因此您还需要一个循环索引(j) 您不需要再使用一个循环来删除非字母数字字符,只需使用循环即可, 此外,您还需要在修剪完成后终止字符串,否则您的字符串
void convert_to_camel(字符*短语){
int j=0;
对于(int i=0;i您没有正确地处理删除\uu
的操作,因此您还需要一个循环索引(j
)
您不需要再使用一个循环来删除非字母数字字符,只需使用
循环即可,
此外,您还需要在修剪完成后终止字符串
,否则您的字符串
将有垃圾字符
void toCamelCase(char* phrase){
int j=0;
for (int i=0;i<strlen(phrase);i++){
if(phrase[i] != '_' && isalnum(phrase[i])){ //Copy Alpha numeric chars not including _.
phrase[j++] = phrase[i];
}
else if(phrase[i] == '_'){
phrase[j++] = toupper(phrase[i+1]);
i++;
}
}
phrase[j] = '\0'; //Terminate the string
}
void toCamelCase(字符*短语){
int j=0;
对于(int i=0;i,类似这样的东西可能会有所帮助:
void toCamelCase(char* phrase){
int length = strlen(phrase);
int res_ind = 0;
for (int i = 0; i < length ; i++) {
// check for underscore in the sentence
if (phrase[i] == '_') {
// conversion into upper case
phrase[i + 1] = toupper(s[i + 1]);
continue;
}
// If not space, copy character
else
phrase[res_ind++] = s[i];
}
phrase[res_ind] = '\0';
}
void toCamelCase(字符*短语){
int长度=strlen(短语);
int res_ind=0;
对于(int i=0;i
到目前为止提供的所有其他解决方案将“
转换为空字符串,并从字符串末尾删除
#include <stddef.h>
#include <ctype.h>
void to_camel_case(char *str) // pun_intended
{
for (size_t i = 0, k = 0; str[i]; ++i, ++k)
{
while (k && str[k] == '_' && str[k + 1]) // 1)
str[k] = k - 1 ? toupper((char unsigned)str[++k]) : str[++k]; // 2)
str[i] = str[k];
}
}
#包括
#包括
void to_camel_case(char*str)//pun_
{
对于(大小i=0,k=0;str[i];++i,++k)
{
而(k&&str[k]='.'&&str[k+1])//1)
str[k]=k-1?toupper((字符无符号)str[++k]):str[++k];//2)
str[i]=str[k];
}
}
跳过连续的”
。确保至少在字符串的开头和结尾保留一个(如果存在)。
将“
替换为下一个字符,必要时大写
请注意,在当前的实现中,您试图使用new
字符数组来存储处理后的字符串,但您将无法在该函数之外使用它,因为它是局部变量,并且它的生命周期就是流退出该函数的那一刻
以下是我对这一职能的建议:
#define MAX_STR_LEN 50
// assuming that 'str' is a null terminated string
void to_camel_case(char *str)
{
int idx = 0;
int newIdx = 0;
int wasUnderscore = 0;
// just to be on the safe side
if (!str || strlen(str) >= MAX_STR_LEN)
return;
while (str[idx])
{
if (str[idx] == '_')
{
idx++;
// no copy in this case, just raise a flag that '_' was met
wasUnderscore = 1;
}
else if (wasUnderscore)
{
// next letter after the '_' should be uppercased
str[newIdx++] = toupper(str[idx++]);
// drop the flag which indicates that '_' was met
wasUnderscore = 0;
}
else
{
// copy the character and increment the indices
str[newIdx++] = str[idx++];
}
}
str[newIdx] = '\0';
}
我用了一些输入,这就是我得到的:
String hello_world became helloWorld
String hello___world became helloWorld
String hel_lo_wo_rld__ became helLoWoRld
String __hello_world__ became HelloWorld
需要复制一个索引变量(<代码> j>代码>)。什么是<代码> FuluLLEN <代码>?什么是代码> Word < /代码>?请提供一个完整的代码示例。。它应该转换成什么?将原始字符串和转换后的字符串放在另一个字符串下面。关于它们对应字符之间的偏移量,您能说些什么呢?(int)
强制转换为str[k]=toupper((int)str[++k]);
不能防止带有带符号的普通char
类型的重音字符(升级时转换为负int
的字符值)。您需要转换为(无符号字符)
.Casting toint
将负字符值保留为负整数值,唯一可以安全传递的负值是EOF。continue;
没有提供太多好处,因为如果省略它,代码将在循环的下一个循环中继续。
String hello_world became helloWorld
String hello___world became helloWorld
String hel_lo_wo_rld__ became helLoWoRld
String __hello_world__ became HelloWorld