如何比较两个字符数组?

时间:2019-05-17 18:33:11

标签: c string logical-operators

在我的代码中,我在if语句中遇到问题。我觉得代码是正确的。的 检索第一个字符数组的ASCII值。但是,ASCII值 第二个字符数组的总和保持为0。 帮我解决这个问题。

#include<stdio.h>
#include<string.h>
void main()
{
  char team1[50],team2[50];
  int testcase,rounds,i=0,j,l,count=0,ascii1,ascii2;
  scanf("%d",&testcase);
  while(i<testcase)
  {
    scanf("%d",&rounds);
    scanf("%s",team2);
    scanf("%s",team1);
    printf("team2..%s\n",team2);

    for(l=0;l<rounds;l++)
    {
      for(j=l;j<rounds;j++); //<--- Don't do this...
      {
      ascii1=team1[l];
      ascii2=team2[j];
      if(ascii1==ascii2)   
      {
        count+=1;
      }
      printf("count..%d\n",count);
      }
    }
    printf("%d\n",count);
    i++;    
  }
}

应显示相同字符的数量。 例如: 输入:

1
4
asdf
qwsa

输出:

2

2 个答案:

答案 0 :(得分:4)

在第二个;之后,您有一个多余的(错误的)分号(for),使其在一个空块上循环。删除它,就可以了。

答案 1 :(得分:1)

for(l=0;l<rounds;l++)
{
ascii1=team1[l];
for(j=l;j<rounds;j++)
{
ascii2=team2[j];
if(ascii1==ascii2)   
{
count+=1;
}
}
}

这仅适用于您给定的输入 ( 1个 4 音频文件 w ) 如果您要更改输入,则代码也将像输入一样更改 asdf和qsas

相关问题