函数mycmp(char *s,char *t)的功能是比较字符串s和t的大小,当s等于t时返回0,当s>t时返回正值,当s<t时返回负值,请填空。mycmp( char *s,char *t){ while (*s==*t) { if (*s==’\0’)return 0; ++s;++t; } return();}
第1题:
下列给定程序中,函数fun()的功能是:从s所指字符串中,找出t所指字符串的个数作为函数值返回。例如,当s所指字符串中的内容为abcdabfab,t所指字符串的内容为ab,则函数返回整数3。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构.
试题程序:
include <conio.h>
include <stdio.h>
include <string.h>
int fun (char *s, char *t)
{int n; char *p, *r;
n=0;
while(*s)
{p=s;
r=t;
while (*r)
/**************found**************/
if(*r==*p) {r++; p++}
else break;
/*************found**************/
if(r=='\0')
n++;
s++;
}
return n;
}
main()
{char s[100], t[100]; int m;
clrscr();
printf("\nPlease enter string s: ");
scanf ("%s",s);
printf("\nPlease enter substring t: ");
scanf ("%s",t);
m=fun (s,t);
printf("\nThe result is: m=%d\n", m);
}
第2题:
(选做题)查找字符串。输入两个字符串s和t,在字符串 s中查找子串t,输出起始位置,若不存在则输出-1。要求自定义函数char *search(char *s,char *t)返回子串t的首地址,若未找到,则返回NULL。
第3题:
以下函数的功能是将两个字符串s和t连接起来,横线部分应该填写什么语句? void conj(char *s,char *t) { char *p=s; while(*s) _______; while(*t) {*s= *t;s++;t++ } *s='0'; }
A.*s
B.s
C.s++
D.s--
第4题:
函数strcmp( )的功能是对两个字符串进行比较,当s所指字符串和t所指字符串相等时,返回值为0;
当s所指字符串大于t所指字符串时,返回值大于0;当s所指字符串小于t所指字符串时,返回值小于
0(功能等同于库函数strcmp( ) ),请填空。
include <stdio.h>
int strcmp ( chat * s, char * t)
{ while( * s && * t && * s=【 】
{ s++;t++; }
return 【 】;
}
第5题:
以下函数的功能是将两个字符串s和t连接起来,横线部分应该填写什么语句? void conj(char *s,char *t) { char *p=s; while(*s) ; while(*t) {*s= *t;s++;t++ } *s='0'; }
A.*s
B.s
C.s++
D.s--