normalize
文字列を処理する準備をする関数を書いています。これはコードです:
/* The normalize procedure examines a character array of size len
in ONE PASS and does the following:
1) turns all upper-case letters into lower-case ones
2) turns any white-space character into a space character and,
shrinks any n>1 consecutive spaces into exactly 1 space only
3) removes all initial and final white-space characters
Hint: use the C library function isspace()
You must do the normalization IN PLACE so that when the procedure
returns, the character array buf contains the normalized string and
the return value is the length of the normalized string.
*/
int normalize(char *buf, /* The character array containing the string to be normalized*/
int len /* the size of the original character array */)
{
/* exit function and return error if buf or len are invalid values */
if (buf == NULL || len <= 0)
return -1;
char *str = buf;
char prev, temp;
len = 0;
/* skip over white space at the beginning */
while (isspace(*buf))
buf++;
/* process characters and update str until end of buf */
while (*buf != '\0') {
printf("processing %c, buf = %p, str = %p \n", *buf, buf, str);
/* str might point to same location as buf, so save previous value in case str ends up changing buf */
temp = *buf;
/* if character is whitespace and last char wasn't, then add a space to the result string */
if (isspace(*buf) && !isspace(prev)) {
*str++ = ' ';
len++;
}
/* if character is NOT whitespace, then add its lowercase form to the result string */
else if (!isspace(*buf)) {
*str++ = tolower(*buf);
len++;
}
/* update previous char and increment buf to point to next character */
prev = temp;
buf++;
}
/* if last character was a whitespace, then get rid of the trailing whitespace */
if (len > 0 && isspace(*(str-1))) {
str--;
len--;
}
/* append NULL character to terminate result string and return length */
*str = '\0';
return len;
}
ただし、セグメンテーション違反が発生しています。問題を次の行に絞り込みました。
*str++ = *buf;
具体的には、 str を参照して新しい char 値 (例: *str = c
) を割り当てようとすると、プログラムがクラッシュします。ただしstr
、最初に指すbuf
ように初期化されたので、なぜこれが起こっているのかわかりません。
*編集:これは私が関数を呼び出す方法です:*
char *p = "string goes here";
normalize(p, strlen(p));