下列给定程序中函数fun的功能是:将s所指字符串中出现的与t1所指字符串相同的了串全部替换为t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。 例如,当s所指字符串中的内容为“abcdabfab”,t1所指子串中的

admin2020-11-27  20

问题 下列给定程序中函数fun的功能是:将s所指字符串中出现的与t1所指字符串相同的了串全部替换为t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。
    例如,当s所指字符串中的内容为“abcdabfab”,t1所指子串中的内容为“ab”,t2所指子串中的内容为“99”时,在W所指的数组中的内容应为“99cd99t99”。
    请改正程序中的错误,使它能得出正确的结果。
    注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
  试题程序:
#inclucle<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void fun(char*s,char*t1,char*t2,
char*w)
{
    char*P,*r,*a;
strcpy(w,s);
    while(*w)
    {
    P=w;r=t1;
/**********found***********/
    while(r)
    if(*r==*P){r++,P++,)
    else break,
    if(*r==‘\0’)
    {
    a=w;r=t2;
    while(*r)
/**********found***********/
    {*a=*r;a++;r++}
    w+=strlen(t2);
    }
    else w++:
    }
}
void main()
{
  char s[100],t1[100],t2[100],
w[100];
    system(“CLS”),
    printf(“\nPlease  enter
string S:”);
    scanf(“%s”,s),
    printf  (“\nPlease  enter substring t1:”);
    scanf(“%s”,t1),
    printf  (“\nPlease  enter
substring t2:”);
    scanf(“%s”,t2);
    if(strlen(t1)==strlen(t2))
    {
    fun(s,t1,t2,w);
    printf(“\nThe result is:%s
\n”,w),
    }
    else
    printf(“Error:strlen(t2)\n”),
}

选项

答案(1)while(*r) (2)*a=*r;a++;r++;

解析 while(r)和r++都是简单的逻辑和语法错误,C语言中语句必须以分号“;”结尾。只要掌握了C语言的基础知识,发现这样的错误是很容易的。
转载请注明原文地址:https://kaotiyun.com/show/pZ3p777K
0

随机试题
最新回复(0)