我正在用 c 語言撰寫代碼來搜索字串中的一些名稱。任務是我可以搜索名稱,即使它有小寫和大寫。
例子:
How many names do you want to input? 3
Input Names:
John Harrow
Dean
Michael Jackson
Input the name you searching : john harrOW
john harrOW found in index-0
我已經撰寫了代碼,但是有一些錯誤:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void lowercase_words(str)
{
int i;
for(i=0;i<=strlen(str);i ){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i] 32;
}
}
int main()
{
int a;
char b[255];
int c = 0;
char str[];
printf("How many names do you want to input? ");
scanf("%d", &a);
char ignore;
scanf("%c", &ignore);
char names [a][255];
int j;
for(j = 0; j < a; j ){
scanf("%[^\n]", names[j]);
lowercase(names[j]);
scanf("%c", &ignore);
}
printf("Input the name you searching: ");
scanf("%[^\n]", b)
lowercase(b[])
int i;
for(i = 0; i < a; i )
{
if(strcmp(b, names[i]) == 0)
{
c = 1;
printf("%[^\n] found in index-%d", b, i);
break;
}
}
if (c == 0)
{
printf("we cant found %[^\n]", b);
}
return 0;
}
錯誤是:
- 變數或欄位“lowercase_words”宣告為無效
- 'str' 未在此范圍內宣告
謝謝你。
uj5u.com熱心網友回復:
你寫了:
void lowercase_words(str)
您必須為函式的每個引數提供一個型別:
void lowercase_words(char* str) /* Added type "pointer-to-characters" */
我建議將功能更改為:
void lowercase_words(char* str)
{
for(int i=0;i<strlen(str);i ){
str[i] = tolower(str[i]);
}
}
uj5u.com熱心網友回復:
我會盡力幫助你學習。
關于您的錯誤,我將在此處重新列印帶有行號的原始代碼,以便更容易解決:
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4
5 void lowercase_words(str)
6 {
7 int i;
8 for(i=0;i<=strlen(str);i ){
9 if(str[i]>=65&&str[i]<=90)
10 str[i]=str[i] 32;
11 }
12 }
13
14 int main()
15 {
16 int a;
17 char b[255];
18 int c = 0;
19 char str[];
20
21 printf("How many names do you want to input? ");
22 scanf("%d", &a);
23
24 char ignore;
25 scanf("%c", &ignore);
26
27 char names [a][255];
28
29 int j;
30 for(j = 0; j < a; j ){
31 scanf("%[^\n]", names[j]);
32 lowercase(names[j]);
33 scanf("%c", &ignore);
34 }
35
36 printf("Input the name you searching: ");
37 scanf("%[^\n]", b)
38 lowercase(b[])
39
40
41 int i;
42 for(i = 0; i < a; i )
43 {
44 if(strcmp(b, names[i]) == 0)
45 {
46 c = 1;
47 printf("%[^\n] found in index-%d", b, i);
48 break;
49 }
50 }
51
52 if (c == 0)
53 {
54 printf("we cant found %[^\n]", b);
55 }
56
57 return 0;
58 }
和你原來陳述的錯誤:
Variable or field 'lowercase_words' declared void
and
'str' was not declared in this scope
這看起來像是顯示代碼之前的錯誤。也許您的編譯器在第 5 行的代碼上按原樣生成了此錯誤。盡管如此,第 5 行的問題是您指定了一個名為 lowercase_words 的函式,并且您說這個特定的函式有一個由呼叫者提供的引數名為str. 但是,函式引數需要變數型別和名稱。考慮型別來指定一個包含變數型別的框,并且名稱是框的名稱。str在這種情況下,您需要一個名稱為 char 陣列或字串型別的變數。
所以,而不是:
void lowercase_words(str)
做
void lowercase_words(char str[255])
請注意,我使用 char 陣列為 255 個字符,因為您有第 17 行,其中 b 為 255,名稱為 255。
順便說一句,它更喜歡使用常量而不是硬編碼的數字。這就是其他海報在說“幻數”時所說的。所以,你可以這樣做:
const int mylen = 255;
void lowercase_words(char str[mylen])
但是,這對您不起作用,因為在函式中,您正在修改 str 的值。IE。在第 10 行,默認情況下,您執行str[i]=str[i] 32;
函式按值傳遞函式引數。如果要修改函式引數值,則需要pass by reference使用pass by value. 在這種情況下,您需要使用指標。
void lowercase_words(char *pstr)
在這種情況下,我將更改您的變數名稱以使用p前綴來指定其指標。這樣,函式引數執行和輸入/輸出變數。
下一個修復是在第 37 行和第 38 行,您需要在其中包含一個結束分號。b[]用作引數的變數也被修改為 simple b。同樣在第 19 行,strchar 陣列需要一個大小。為簡單起見,我將再次使用您的 255 尺寸。
在第 32 和 38 行,您使用了一個名為lowercase. 相信你打算用lowercase_words.
因此,這消除了您的編譯錯誤。但是,您有一些邏輯錯誤會阻止此代碼正確運行。在我們解決這個問題之前,讓我們再看看現在的修改與行號。
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4
5 void lowercase_words(char *pstr)
6 {
7 int i;
8 for(i=0;i<=strlen(pstr);i ){
9 if(pstr[i]>=65&&pstr[i]<=90)
10 pstr[i]=pstr[i] 32;
11 }
12 }
13
14 int main()
15 {
16 int a;
17 char b[255];
18 int c = 0;
19 char str[255];
20
21 printf("How many names do you want to input? ");
22 scanf("%d", &a);
23
24 char ignore;
25 scanf("%c", &ignore);
26
27 char names [a][255];
28
29 int j;
30 for(j = 0; j < a; j ){
31 scanf("%[^\n]", names[j]);
32 lowercase_words(names[j]);
33 scanf("%c", &ignore);
34 }
35
36 printf("Input the name you searching: ");
37 scanf("%[^\n]", b);
38 lowercase_words(b);
39
40
41 int i;
42 for(i = 0; i < a; i )
43 {
44 if(strcmp(b, names[i]) == 0)
45 {
46 c = 1;
47 printf("%[^\n] found in index-%d", b, i);
48 break;
49 }
50 }
51
52 if (c == 0)
53 {
54 printf("we cant found %[^\n]", b);
55 }
56
57 return 0;
58 }
所以,我認為這就是您希望輸出的樣子:
myhost:~$ gcc so9229.c
myhost:~$ ./a.out
How many names do you want to input? 2
You specified 2 names
JOHN
DAVIS
names[0] = john
names[1] = davis
Input the name you searching: JOHN
You wish to search for john
Found at index:0, the name john
myhost:~$ ./a.out
How many names do you want to input? 2
You specified 2 names
JOHN
DAVIS
names[0] = john
names[1] = davis
Input the name you searching: TOM
You wish to search for tom
Cant find tommyhost:~$
話雖如此,這是修改代碼的最終結果。我希望這對您有所幫助并祝您好運,因為您將繼續學習。太棒了,我知道你會做得很好!
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void lowercase_words(char *pstr)
{
int i;
for(i=0;i<=strlen(pstr);i ){
if(pstr[i]>=65&&pstr[i]<=90)
pstr[i]=pstr[i] 32;
}
}
int main()
{
int a;
char b[255];
int c = 0;
char str[255];
printf("How many names do you want to input? ");
scanf("%d", &a);
// dump the number of names
printf("\nYou specified %d names\n ", a);
char ignore;
scanf("%c", &ignore);
char names [a][255];
int j;
for(j = 0; j < a; j ){
scanf("%[^\n]", names[j]);
lowercase_words(names[j]);
scanf("%c", &ignore);
}
// dump the list of names
for(j = 0; j < a; j ){
printf("names[%d] = %s\n",j, names[j]);
}
printf("Input the name you searching: ");
scanf("%[^\n]", b);
// dump the name you are searching for
lowercase_words(b);
printf("\nYou wish to search for %s\n ", b);
int i;
for(i = 0; i < a; i )
{
if(strcmp(b, names[i]) == 0)
{
c = 1;
printf("Found at index:%d, the name %s\n", i, b);
break;
}
}
if (c == 0)
{
printf("Cant find %s", b);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/468824.html
標籤:C
