C语言中查找字符串中字符的堆栈 #包括 #包括 main() { int i; int*b,*z; 字符名[30]; 字符元音[5]={'A','E','I','O','U'}; 字符辅音[23]={B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'}; printf(“输入字符串:”); scanf(“%s”,名称); printf(“字符串为%s\n”,名称); 对于(i=0;名称[i]!='\0';i++){ 如果 (b=strchr(元音,名称[i])!=NULL){ printf(“元音是:%s\n”,b);} 否则如果 (z=strchr(辅音,名称[i])!=NULL){ printf(“辅音是:%s\n”,z); } } }

C语言中查找字符串中字符的堆栈 #包括 #包括 main() { int i; int*b,*z; 字符名[30]; 字符元音[5]={'A','E','I','O','U'}; 字符辅音[23]={B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'}; printf(“输入字符串:”); scanf(“%s”,名称); printf(“字符串为%s\n”,名称); 对于(i=0;名称[i]!='\0';i++){ 如果 (b=strchr(元音,名称[i])!=NULL){ printf(“元音是:%s\n”,b);} 否则如果 (z=strchr(辅音,名称[i])!=NULL){ printf(“辅音是:%s\n”,z); } } },c,arrays,string,C,Arrays,String,我试图找出数组中有多少元音和辅音。这是我们老师教给我们的唯一算法,但它不起作用。有人能指出我的错误吗 我刚刚又试了一次,听了你的建议 #include <stdio.h> #include <string.h> main() { int i; int *b, *z; char name[30]; char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; char consonants[23] = {'B','C','D',

我试图找出数组中有多少元音和辅音。这是我们老师教给我们的唯一算法,但它不起作用。有人能指出我的错误吗

我刚刚又试了一次,听了你的建议

#include <stdio.h>
#include <string.h>

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);
    }
  }
}
#包括
#包括
int main()
{
我发誓,反对者,我;
字符*s,*s1;
字符名[30];
字符元音[6]=“AEIOU”;
字符辅音[21]=“BCDFGHJKLMNPQRSTVWXYZ”;
printf(“输入字符串:”);
scanf(“%s”,名称);
printf(“字符串为%s\n”,名称);
对于(i=0;名称[i]!='\0';i++)
s=strchr(元音,名称[i]);
printf(“元音是:%s\n”,s);
s1=strchr(辅音,名称[i])){
printf(“辅音是:%s\n”,s1);
}
返回0;
}
这就是我改变它的方式,有了你的建议,我还有什么问题?因为还是不行。 谢谢

这是我的另一个版本的程序

#include <stdio.h>
#include <string.h>

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;

}
#包括
#包括
int main()
{
int i;
int计数器=0,计数器2=0;
char*s;
字符名[30];
字符元音[6]=“AEIOU”;
字符辅音[21]=“BCDFGHJKLMNPQRSTVWXYZ”;
printf(“输入字符串:”);
scanf(“%s”,名称);
printf(“字符串为%s\n”,名称);
对于(i=0;名称[i]!='\0';i++){
if(s=strchr(元音,名称[i])){
计数器++;
}
else if(s=strchr(辅音,名称[i])){
计数器2++;
}
printf(“第一个计数器是%d\n”,计数器);
printf(“第二个计数器是%d\n”,计数器2);
返回0;
}
}
我添加了计数器来计算元音和辅音的数量,但仍然不起作用。

strchr()
用于在字符串中搜索

 #include <stdio.h>
 #include <string.h>

 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()
用于在字符串中搜索

 #include <stdio.h>
 #include <string.h>

 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;
 }
  }

当我编译此文件时,会收到以下消息:

char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
因此,首先确保main的返回类型为“int”

$ 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]
并在函数的底部添加一个返回

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

return 0;
这将消除所有警告

char *b, *z;
好极了。现在,当我们运行您的程序时:

$ gcc -Wall vc.c
$
“分段错误”意味着您正在运行一个数组的末尾并读取您不拥有的内存。现在实施伊格纳西奥·巴斯克斯·艾布拉姆斯的解决方案

$ ./a.out 
input the string: aaa
The string is aaa
Segmentation fault
现在,您的程序将运行到完成

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
然而,如果您试图将它们输出为列表,那么您将有更多的数据操作要做。查看以下链接:


编译此文件时,我收到以下消息:

char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
因此,首先确保main的返回类型为“int”

$ 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]
并在函数的底部添加一个返回

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

return 0;
这将消除所有警告

char *b, *z;
好极了。现在,当我们运行您的程序时:

$ gcc -Wall vc.c
$
“分段错误”意味着您正在运行一个数组的末尾并读取您不拥有的内存。现在实施伊格纳西奥·巴斯克斯·艾布拉姆斯的解决方案

$ ./a.out 
input the string: aaa
The string is aaa
Segmentation fault
现在,您的程序将运行到完成

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
然而,如果您试图将它们输出为列表,那么您将有更多的数据操作要做。查看以下链接:


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

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

您也不需要定义
辅音。不是元音的是辅音

这是密码。我已经对它进行了测试,它可以工作:

printf("Vowels:\t%d\nConsonants:\t%d", vowelsFound, consonantsFound);
#包括
#包括
#包括
int main()
{
int i;
int计数器=0,计数器2=0;
char*s;
字符名[30];
字符元音[6]=“AEIOU”;
printf(“输入字符串:”);
scanf(“%s”,名称);
printf(“字符串为%s\n”,名称);
对于(i=0;名称[i]!='\0';i++){
if(strhr(元音,toupper(名称[i]))!=NULL){
计数器++;
}
否则{
计数器2++;
}
}
printf(“第一个计数器是%d\n”,计数器);
printf(“第二个计数器是%d\n”,计数器2);
返回0;
}

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

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

您也不需要定义
辅音。不是元音的是辅音

这是密码。我已经对它进行了测试,它可以工作:

printf("Vowels:\t%d\nConsonants:\t%d", vowelsFound, consonantsFound);
#包括
#包括
#包括
int main()
{
int i;
int计数器=0,计数器2=0;
char*s;
字符名[30];