有沒有辦法檢查一個字母是否出現在字串中。我正在嘗試以下內容,但它似乎只掃描第一個字母,即使它被放入 for 回圈中。有什么建議?
//function prototypes
char checkifletterisinword();
char guess();
int main()
{
char *thesecretword[15];
char guessedletter;
char *guessedword[15];
*thesecretword = "stackoverflow";
guessedletter = guess();
checkifletterisinword( thesecretword, guessedletter );
return 0;
};
//----functions
char checkifletterisinword( char **thesecretword, char *guessedletter )
{
for ( int i=0; i!='\n'; i )
{
if ( guessedletter == *thesecretword[i] )
{
printf( "The letter %c is in the word", guessedletter );
}
else
{
printf( "The letter %c is not in word", guessedletter );
}
return 0;
}
};
char guess()
{
char temp;
printf( "Guess a letter...\n" );
temp = getchar();
getchar();
return temp;
};
uj5u.com熱心網友回復:
是的,使用該strchr功能。
語法: char *strchr(const char *str, int character);. 第一個引數應該是您需要檢查的字串,第二個引數應該是字符。
#include <stdio.h>
#include <string.h>
int main ()
{
char string[55] ="This is a string for testing";
char *p;
p = strchr (string,'i');
printf ("Character i is found at position %d\n",p-string 1);
printf ("First occurrence of character \"i\" in \"%s\" is" \
" \"%s\"",string, p);
return 0;
}
uj5u.com熱心網友回復:
以下幾行是錯誤的:
char *thesecretword[15];
[...]
*thesecretword = "stackoverflow";
線
*thesecretword = "stackoverflow";
相當于:
thesecretword[0] = "stackoverflow";
這意味著您只是設定thesecretword[0],而不是thesecretword[1]設定thesecretword[14]。
您可能只需要一個指標,因此創建一個包含 15 個指標的陣列是沒有意義的。因此,您可能應該將這些行更改為以下內容:
char *thesecretword;
[...]
thesecretword = "stackoverflow";
另外,函式宣告
char checkifletterisinword( char **thesecretword, char *guessedletter )
沒有意義。回傳型別可能應該是bool指示是否找到了該字母。此外,第一個引數應該是指向要搜索的字串的指標,而不是指向指標的指標。此外,第二個引數應該是要搜索的字符的值,而不是指向字符的指標。
因此,您可能應該將函式宣告和定義更改為以下內容:
bool checkifletterisinword( char *thesecretword, char guessedletter )
請注意,您必須#include <stdbool.h>為了使用該型別bool。
線
for ( int i=0; i!='\n'; i )
也沒有意義。回圈條件i!='\n'似乎沒有意義,因為字串由空終止字符而不是換行符終止。此外,您不想比較索引i本身,而是比較該索引處的字符。因此,您可能應該將該行更改為以下內容:
for ( int i=0; thesecretword[i]!='\0'; i )
由于我們更改了函式的引數,因此checkifletterisinword該行
if ( guessedletter == *thesecretword[i] )
不再有意義。我們應該將其更改為以下內容:
if ( guessedletter == thesecretword[i] )
另外,線
return 0;
在函式checkifletterisinword中沒有意義。return true;如果找到了字母,并且return false在比較字串的所有字母但沒有找到匹配項之后,我們想要。所以函式應該是這樣的:
bool checkifletterisinword( char *thesecretword, char guessedletter )
{
for ( int i=0; thesecretword[i]!='\0'; i )
{
if ( guessedletter == thesecretword[i] )
{
return true;
}
}
return false;
}
根據回傳的值,我們可以在函式中列印適當的訊息main:
if ( checkifletterisinword( thesecretword, guessedletter ) )
{
printf( "The letter %c is in the word.\n", guessedletter );
}
else
{
printf( "The letter %c is not in word.\n", guessedletter );
}
此外,您所有的函式定義都有一個尾隨;. 這在 ISO C 中是不允許的。
完成所有這些更改后,您的整個程式應如下所示:
#include <stdio.h>
#include <stdbool.h>
bool checkifletterisinword( char *thesecretword, char guessedletter );
char guess();
int main()
{
char *thesecretword;
char guessedletter;
thesecretword = "stackoverflow";
guessedletter = guess();
if ( checkifletterisinword( thesecretword, guessedletter ) )
{
printf( "The letter %c is in the word.\n", guessedletter );
}
else
{
printf( "The letter %c is not in word.\n", guessedletter );
}
return 0;
}
bool checkifletterisinword( char *thesecretword, char guessedletter )
{
for ( int i=0; thesecretword[i]!='\0'; i )
{
if ( guessedletter == thesecretword[i] )
{
return true;
}
}
return false;
}
char guess()
{
char temp;
printf( "Guess a letter...\n" );
temp = getchar();
getchar();
return temp;
}
該程式具有以下行為:
Guess a letter...
a
The letter a is in the word.
Guess a letter...
b
The letter b is not in word.
請注意,您可以簡單地使用該函式strchr在字串中搜索特定字符。您不需要為此撰寫自己的函式:
#include <stdio.h>
#include <string.h>
char guess();
int main()
{
char *thesecretword;
char guessedletter;
thesecretword = "stackoverflow";
guessedletter = guess();
if ( strchr( thesecretword, guessedletter ) )
{
printf( "The letter %c is in the word.\n", guessedletter );
}
else
{
printf( "The letter %c is not in word.\n", guessedletter );
}
return 0;
}
char guess()
{
char temp;
printf( "Guess a letter...\n" );
temp = getchar();
getchar();
return temp;
}
該程式具有與第一個程式相同的行為。
uj5u.com熱心網友回復:
現在忘記回圈。您的主要問題是指標不像您認為的那樣作業。以下內容遠不是一個完整的教程,但希望它能讓您足夠了解您需要提出哪些后續問題。
char字串是以 .結尾的陣列\0。- 雙引號中的字串文字將自動添加尾隨
\0; 您無需手動操作。 char foo[15]char宣告一個 15秒的陣列。char *foo保存 a 的地址char。如果將它指向一個字串,它會保存字串中第一個字符的地址。然后,您可以從那里向前回圈,直到您到達\0標記結束的地方。*foo接受一個指標并計算它指向的東西。因此,在字串的情況下,它計算為第一個字符foo[0].- 索引運算子
foo[idx]等價于*(foo idx). 因此,您可以通過增加索引或通過增加指標本身來回圈字串,以使其指向陣列的下一個元素:idx ; foo[idx]并且foo ; foo[idx]都將導致訪問字串的元素 1。 - 指標知道它們指向的東西的大小。這對字串無關緊要,因為
char它被定義為 1 個位元組,但是如果你有int foo[42], 并且 int 是 4 個位元組長,foo[1]并且從陣列的開頭*(foo 1)看起來是 1 ,而不是一個位元組。sizeof(int)所以它會按照你期望的方式作業。 - 允許用戶寫超出陣列的末尾是非常糟糕的。(具體來說,它允許攻擊者在您的系統上運行任意代碼,但其機制超出了此處的范圍。)任何時候您閱讀用戶輸入時,請始終檢查他們輸入的字符是否超過您的空間.
所以在你的代碼中:
char *thesecretword[15];char宣告一個由 15 個指標組成的陣列,而不是 15char秒。*thesecretword = "stackoverflow";將“stackoverflow\0”放在記憶體中某處,并將地址放在thesecretword[0].thesecretword[1]通過thesecretword[14]保持未初始化。
這是您宣告要宣告的變數的方式:
#define MAX_LEN 15 // When putting user input into `guessedword[i]`, we'll only permit it if `i < MAXLEN`
char *thesecretword = "stackoverflow";
char guessedword[MAX_LEN];
uj5u.com熱心網友回復:
您可以使用該strchr功能,也可以自己實作它。在您的代碼中,條件i!='\n'不正確,因為您將int變數與字符進行比較。然后,在for你回傳 0 的內部(注意函式希望你回傳一個字符的事實),這就是為什么你只考慮第一個字母。
你可以這樣做:
int checkifletterisinword(char *thesecretword, char guessedLetter) {
for(int i=0;thesecretword[i]!='\0';i ){
if(thesecretword[i]==guessedLetter){
return 1;
}
}
return 0;
}
我更改了函式原型,因為您以錯誤的方式使用指標。
如果字符不在字串中,此函式回傳 0,否則回傳 1。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427887.html
標籤:C
下一篇:訪問c中分配的記憶體時出錯
