用C语言在字符串中查找字符的堆栈

#include  #include  main() { int i; int *b, *z; char name[30]; char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; char consonants[23] = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'}; printf ("input the string: "); scanf ("%s", name); printf ("The string is %s\n", name); for (i=0; name[i]!='\0'; i++){ if (b=strchr(vowel, name[i]) != NULL) { printf ("The vowels are: %s\n", b); } else if (z=strchr(consonants, name[i]) != NULL) { printf ("The consonants are: %s\n", z); } } } 

我试图在arrays中找到多少个元音和辅音。 这是我们老师向我们展示的唯一算法,但它不起作用。 任何人都可以指出我的错误?

我只是再试一次,提出你的所有建议,

 #include  #include  int main() { int vow, cons, i; char *s, *s1; char name[30]; char vowel[6] = "AEIOU"; char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ"; printf ("input the string: "); scanf ("%s", name); printf ("The string is %s\n", name); for (i=0; name[i]!='\0'; i++) s = strchr(vowel, name[i]); printf ("The vowels are: %s\n", s); s1 =strchr(consonants, name[i])) { printf ("The consonants are: %s\n", s1); } return 0; } 

这就是我改变它的方式,包括你的所有建议,我的其他问题是什么? 因为仍然不能正常工作。 谢谢。

这是我的另一个程序版本

  #include  #include  int main() { int i; int counter=0, counter2=0; char *s; char name[30]; char vowel[6] = "AEIOU"; char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ"; printf ("input the string: "); scanf ("%s", name); printf ("The string is %s\n", name); for (i=0; name[i]!='\0'; i++) { if (s = strchr(vowel, name[i])) { counter++; } else if (s =strchr(consonants, name[i])) { counter2++; } printf ("First counter is %d\n", counter); printf ("The second counter is %d\n", counter2); return 0; } } 

我添加了计数器来计算元音和辅音的数量,但仍然不起作用。

strchr()用于搜索字符串。

 char vowel[] = "AEIOU"; char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ"; 
 #include< stdio.h> int main() { int vowel=0,consonant=0; printf ("input the string: "); scanf ("%s", name); printf ("The string is %s\n", name); for(int i=0;name[i] !='\0';i++) { if( name[i] == 'A' || name[i] == 'E' || name[i] == 'I' || name[i] == 'O' || name[i] == 'U' ) { vowel++; } else consanant++; } printf("%d %d",vowel,consonant); return 0; } 

当我编译它时,我收到以下消息:

 $ gcc -Wall vc.c vc.c:4:1: warning: return type defaults to 'int' [-Wreturn-type] vc.c: In function 'main': vc.c:17:8: warning: assignment makes pointer from integer without a cast [enabled by default] vc.c:17:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses] vc.c:18:4: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat] vc.c:20:13: warning: assignment makes pointer from integer without a cast [enabled by default] vc.c:20:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses] vc.c:21:4: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat] vc.c:24:1: warning: control reaches end of non-void function [-Wreturn-type] 

因此,首先要确保main的返回类型是’int’

 int main(){ 

并在函数的底部添加一个返回

 return 0; 

此后,将b和z设置为char * s,以便它们与strchr的返回类型匹配

 char *b, *z; 

这将消除所有警告。

 $ gcc -Wall vc.c $ 

优秀。 现在,当我们运行你的程序时:

 $ ./a.out input the string: aaa The string is aaa Segmentation fault 

“分段错误”意味着您正在运行arrays的末尾并读取您不拥有的内存。 现在实施Ignacio Vazquez-Abrams的解决方案

 char vowel[] = "AEIOU"; char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ"; 

现在你的程序将运行完成。

 $ ./a.out input the string: AAA The string is AAA The vowels are: AEIOU The vowels are: AEIOU The vowels are: AEIOU 

但它做得不多,是吗?

所以,如果你只想计算有多少元音和辅音,你可以为每一个添加一个整数,每次找到正确的类型时都会增加,并在结尾处输出它们:

 printf("Vowels:\t%d\nConsonants:\t%d", vowelsFound, consonantsFound); 

但是,如果您尝试将它们作为列表输出,那么您将需要执行更多数据操作。 一些链接要检查:

用于printf的Linux手册页

用于String函数的Linux Man Page

您已将return语句放在for循环中,这会阻止它扫描整个name数组。

使用strchr ,您还需要将当前循环字符转换为大写,以便正确匹配,因为您已经定义了大写的vowels 。 要使用toupper()您需要包含ctype.h

您也不需要定义consonants 。 什么不是元音是辅音。

这是代码。 我测试了它,它的工作原理:

 #include  #include  #include  int main() { int i; int counter=0, counter2=0; char *s; char name[30]; char vowel[6] = "AEIOU"; printf ("input the string: "); scanf ("%s", name); printf ("The string is %s\n", name); for (i=0; name[i]!='\0'; i++) { if (strchr(vowel, toupper(name[i])) != NULL) { counter++; } else { counter2++; } } printf ("First counter is %d\n", counter); printf ("The second counter is %d\n", counter2); return 0; } 

另外。

 #include  #include  int main() { int t [256]; int i,c; int cntw = 0; int cntc = 0; const char * vowel="AEIOUaeiou"; const char * consonants="BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"; memset(t,0,256); while (*vowel) { t[*vowel] = 1; ++vowel;} while (*consonants) { t[*consonants] = 2; ++consonants;} printf ("Input the text: CTRL-D to end\n"); c = getchar(); while(c >=0) { switch(t[c]) { case 1: ++cntw; break; case 2: ++cntc; break; } c=getchar(); } printf ("Text has %d vowel%s and %d consonant%s\n", cntw,(cntw>1)?"s":"", cntc,(cntc>1)?"s":""); return 0; }