首页 > 题库 > 职业考证 > 软考 > 程序员 > 案例题

【说明】
下面的函数isSameChars判断两个字符串(仅由英文字母构成、区分大小写)是否包含相同的字母且同一字母的出现次数也相同。例如,“assd”与“sdsa”就是由1个“a”、2个“s”和1个“d”形成的不同字符串。函数isSameChars的基本处理思路是对第一个字符串中的各字母进行递增计数,对第二个字符串中的各字母进行递减计数,若最后各字母计数器的值都为0,则二者包含相同的字母且同一字母的出现次数也相同。

【问题1】(15分)
阅读试题说明和C代码,填补C代码中的空缺(1)~ (6),将解答写入对应的解答栏内。
【C代码 】

【C代码 】
#include <stdio.h>
#define true 1
#define false 0

int isSameChars(char *, char *);
int strlen(char *s) ;

int main()
{
char s[]= "acdsdsf",t[]= "ssdfcad";

if(isSameChars(s, t))
printf("Yes\n");
else
printf( "No\n");

return 0;
}

int strlen(char *s) //计算字符串的长度
{
int n= 0;
while ( 1 ) ++n;
return n;
}

int isSameChars(char *s, char *t)
{
int slen = strlen(s);
if ( ( 2 ) ) return false;

int i= 0;
while (i<slen && s[i]== t[i]) ++i;

if( ( 3 ) ) return false;

int count[52]={0}; //用于统计字符串中的各字母的数量
//字母与下标的对应关系为:'A'~'Z': 0~25,'a'~'z': 26-51
while (*s) {
if(*s >='A' &&*s<='Z')
++ count[*s -'A'];
else if (*s >='a' &&*s <= 'z')
( 4 );
++s;
}

while (*t) {
if(*t>='A'&&*t<='Z')
--count[*t-'A'];
else if(*t>='a' &&*t<='z')
( 5 );
++t;
}

for(int i=0, i<52, ++i)
if(( 6) ) return false;

return true;
}

参考答案: 查看答案 查看解析 下载APP畅快刷题

相关知识点试题

相关试卷