我正在嘗試使用此自定義函式計算用戶輸入的字串中的單詞數:
int count_words(char* text)
{
int wc = 0;
for(int i = 0,k = strlen(text); i<k;i )
{
if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
{
wc ;
}
}
return wc 1;
}
但我不斷收到此錯誤:
re.c:59:21: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
^~~~~~~
&
re.c:59:48: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
^~~~~~~
&
到底發生了什么?
uj5u.com熱心網友回復:
strcmp需要char *(字串)不是單個char. 如果您使用 strtok` 來計算字串中的單詞數會更好。
這是一個簡單的例子
#include <stdio.h>
#include <string.h>
int main(void) {
char arr[] = "Temp hello bye! No way. Yes";
char *delimeters = " !.";
char *token = strtok(arr, delimeters);
int count = 0;
while (token != NULL) {
count ;
token = strtok(NULL, delimeters);
}
printf("Words = %d", count);
return 0;
}
您也可以使用strpbrk代替strtok。
uj5u.com熱心網友回復:
strcmp 采用以 NULL 結尾的 char 陣列'\0'。對于您的問題,當您找到特殊字符時,您需要增加單詞數。并且您應該分別檢查沒有重復的特殊字符。
/*const special_char[] = {' ','.','!','?',','};*/
int count_words(char* text)
{
uint32_t idx = 0;
int wc = 0;
/*if the first char equal one of special char, don't count that as word*/
for (int i = 1; i < strlen(text); i )
{
/*Special char shouldn't be repeated respectively otherwise, don't count that as word*/
if (
((' ' == text[i]) || ('.' == text[i]) || ('!' == text[i]) || ('?' == text[i]) || (',' == text[i]))
&&
((' ' != text[i - 1]) && ('.' != text[i - 1]) && ('!' != text[i - 1]) && ('?' != text[i - 1]) && (',' != text[i - 1]))
)
{
wc ;
}
}
/*For the last word*/
wc ;
return wc;
}
uj5u.com熱心網友回復:
函式strcmp宣告如下
int strcmp(const char *s1, const char *s2);
如您所見,函式的兩個引數都具有指標型別const char *。
但是在你使用的函式的這個呼叫中
strcmp(" ",text[i])
第二個引數的型別為char。所以編譯器會發出錯誤資訊。
錯誤:不兼容的整數到指標轉換將“char”傳遞給“const char *”型別的引數;
你的函式也有邏輯錯誤。例如,對于空字串,它回傳等于 的單詞數1。
return wc 1;
每個分隔符都算作一個單詞。因此,對于僅包含分隔符的字串,該函式會將其解釋為包含多個單詞。
除此之外,該函式 count_words不得更改傳遞的字串。它應該宣告為
size_t count_words( const char *text );
該函式可以通過以下方式定義,如下面的演示程式所示。
#include <stdio.h>
#include <string.h>
size_t count_words( const char *text )
{
const char *separators = " \t.,!?";
size_t n = 0;
while ( *text )
{
text = strspn( text, separators );
if ( *text )
{
n;
text = strcspn( text, separators );
}
}
return n;
}
int main(void)
{
const char *text = "We, beginners, should help each other!";
printf( "There are %zu words in the text \"%s\".", count_words( text ), text );
return 0;
}
程式輸出是
There are 6 words in the text "We, beginners, should help each other!".
更通用的方法是當函式的用戶可以呼叫它時,他自己指定所需的單詞分隔符。例如
#include <stdio.h>
#include <string.h>
size_t count_words( const char *text, const char *separators )
{
size_t n = 0;
while ( *text )
{
text = strspn( text, separators );
if ( *text )
{
n;
text = strcspn( text, separators );
}
}
return n;
}
int main(void)
{
const char *text = "We, beginners, should help each other!";
printf( "There are %zu words in the string \"%s\".", count_words( text, " \t.,!?" ), text );
return 0;
}
程式輸出與上圖相同
There are 6 words in the text "We, beginners, should help each other!".
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/341536.html
