gpt4 book ai didi

c - 在 C 中返回 char 数组的长度

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:30 24 4
gpt4 key购买 nike

我是 C 语言编程的新手,正在尝试编写一个简单的函数来规范化 char 数组。最后我想返回新 char 数组的长度。我来自 Java,所以如果我犯了看似简单的错误,我深表歉意。我有以下代码:

/* The normalize procedure normalizes a character array of size len 
according to the following rules:
1) turn all upper case letters into lower case ones
2) turn any white-space character into a space character and,
shrink any n>1 consecutive whitespace characters to exactly 1 whitespace

When the procedure returns, the character array buf contains the newly
normalized string and the return value is the new length of the normalized string.

*/
int
normalize(unsigned char *buf, /* The character array contains the string to be normalized*/
int len /* the size of the original character array */)
{
/* use a for loop to cycle through each character and the built in c functions to analyze it */
int i;

if(isspace(buf[0])){
buf[0] = "";
}
if(isspace(buf[len-1])){
buf[len-1] = "";
}

for(i = 0;i < len;i++){
if(isupper(buf[i])) {
buf[i]=tolower(buf[i]);
}
if(isspace(buf[i])) {
buf[i]=" ";
}
if(isspace(buf[i]) && isspace(buf[i+1])){
buf[i]="";
}
}

return strlen(*buf);


}

最后如何返回char数组的长度?另外,我的程序是否正确地完成了我想要的事情?

编辑:我已根据评论对我的程序进行了一些更正。现在正确吗?

/* The normalize procedure normalizes a character array of size len 
according to the following rules:
1) turn all upper case letters into lower case ones
2) turn any white-space character into a space character and,
shrink any n>1 consecutive whitespace characters to exactly 1 whitespace

When the procedure returns, the character array buf contains the newly
normalized string and the return value is the new length of the normalized string.

*/
int
normalize(unsigned char *buf, /* The character array contains the string to be normalized*/
int len /* the size of the original character array */)
{
/* use a for loop to cycle through each character and the built in c funstions to analyze it */
int i = 0;
int j = 0;

if(isspace(buf[0])){
//buf[0] = "";
i++;
}
if(isspace(buf[len-1])){
//buf[len-1] = "";
i++;
}
for(i;i < len;i++){
if(isupper(buf[i])) {
buf[j]=tolower(buf[i]);
j++;
}
if(isspace(buf[i])) {
buf[j]=' ';
j++;
}
if(isspace(buf[i]) && isspace(buf[i+1])){
//buf[i]="";
i++;
}
}

return strlen(buf);


}

最佳答案

执行此类操作的规范方法是使用两个索引,一个用于读取,一个用于写入。像这样:

int normalizeString(char* buf, int len) {
int readPosition, writePosition;
bool hadWhitespace = false;
for(readPosition = writePosition = 0; readPosition < len; readPosition++) {
if(isspace(buf[readPosition]) {
if(!hadWhitespace) buf[writePosition++] = ' ';
hadWhitespace = true;
} else if(...) {
...
}
}
return writePosition;
}

警告:这仅根据给定的长度处理字符串。虽然使用缓冲区 + 长度具有能够处理任何数据的优势,但这不是 C 字符串的工作方式。 C 字符串在其末尾以空字节终止,您的工作是确保空字节位于正确的位置。您提供的代码不处理空字节,我上面提供的缓冲区 + 长度版本也不处理。这种规范化函数的正确 C 实现如下所示:

int normalizeString(char* string) {    //No length is passed, it is implicit in the null byte.
char* in = string, *out = string;
bool hadWhitespace = false;
for(; *in; in++) { //loop until the zero byte is encountered
if(isspace(*in) {
if(!hadWhitespace) *out++ = ' ';
hadWhitespace = true;
} else if(...) {
...
}
}
*out = 0; //add a new zero byte
return out - string; //use pointer arithmetic to retrieve the new length
}

在这段代码中,我用指针替换了索引,只是因为这样做很方便。这只是风格偏好的问题,我可以用明确的索引写同样的东西。 (而且我的风格偏好不是指针迭代,而是简洁的代码。)

关于c - 在 C 中返回 char 数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21863294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com