Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定C中字符数组的长度_C_Arrays - Fatal编程技术网

确定C中字符数组的长度

确定C中字符数组的长度,c,arrays,C,Arrays,我试图确定字符数组的长度。一个字符数组及其长度被输入函数中,并使用它执行一些操作。我试图在事后确定它的大小。现在编写代码的方式是函数返回的长度大于预期。我来自java,所以如果我犯了看似简单的错误,我道歉。我有以下代码: /* The normalize procedure normalizes a character array of size len according to the following rules: 1) turn all upper case lette

我试图确定字符数组的长度。一个字符数组及其长度被输入函数中,并使用它执行一些操作。我试图在事后确定它的大小。现在编写代码的方式是函数返回的长度大于预期。我来自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 = 0;
    int j = 0;
    int k = len;

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

    buf[j] = '\0';

   return k;


}
/*规范化过程规范化大小为len的字符数组
根据以下规则:
1) 将所有大写字母转换为小写字母
2) 将任何空白字符转换为空格字符,
将任意n>1个连续空格字符缩减为正好1个空格
当过程返回时,字符数组buf包含新的
标准化字符串,返回值是标准化字符串的新长度。
*/
int
normalize(unsigned char*buf,/*字符数组包含要规范化的字符串*/
int len/*原始字符数组的大小*/)
{
/*使用for循环循环遍历每个字符,并使用内置的c函数对其进行分析*/
int i=0;
int j=0;
int k=len;
if(isspace(buf[0])){
i++;
k--;
}
if(isspace(buf[len-1])){
i++;
k--;
}
对于(i;i
如果使用
buf[j]='\0'
,则字符串的长度为
j

请记住,这不一定是最初分配的整个阵列(
buf
)的大小

在函数外部,您可以始终调用
strlen(buf)
以获得该长度,或者简单地迭代
buf
,直到遇到0字符:

int i;
for (i=0; buf[i] != 0; i++) // you can use buf[i] != '\0', it's the same thing
{
}
// The length of the string pointed by 'buf' is 'i'

请注意,上面的每个选项都给出了字符串的长度不包括0字符。

@RevanayyaHiremath当
\0
位于索引
j
时,
strlen
返回
j
,而不是
j+1