编写程序:将s字符串中最后一次出现的t1字符串替换成t2字符串(t1字符串和t2所指字符串的长度相同)
编写程序:将s字符串中最后一次出现的t1字符串替换成t2字符串(t1字符串和t2所指字符串的长度相同)。 例如,当s字符串中的内容为:“abcdabfabc”,t1字符串中的内容为:“ab”,t2字符串的内容为:“99”时,结果,在s串内容应为:“abcdabf99c”。
编写程序:将s字符串中最后一次出现的t1字符串替换成t2字符串(t1字符串和t2所指字符串的长度相同)。 例如,当s字符串中的内容为:“abcdabfabc”,t1字符串中的内容为:“ab”,t2字符串的内容为:“99”时,结果,在s串内容应为:“abcdabf99c”。
下列给定的程序中,函数fun()的功能是;将s所指字符串中出现的n所指字符串全部替换成t2所指字符串,所形成的新的字符串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。例如:当s所指字符串中所指的内容为 abcdabfab,t1所指字符串中的内容为ab,t2所指字符串中的内容为99时,结果在w所指的数组中的内容应为99cd99f99。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <stdio.h>
include <string.h>
/*************found**************/
int fun (char *s, char *t1, char *t2, char *w)
{
int i; 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;
/*************found**************/
while (*r){*a=*r;a++;r++}
w+=strlen(t2);
}
else w++;
}
}
main()
{char s[100],t1[100],t2[100],w[100];
clrscr();
printf("\nPlease enter string S: ");
scanf("%s",s);
printf("\nPleaseentersubstring 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");
}
下列给定程序中函数fun()的功能是:求出字符串中最后一次出现的子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为NULL。例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到的信息:Not found!
请改正程序中的错误,使它能得出正确的结果。
注意;不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include<stdio.h>
include <conio.h>
include <string.h>
char *fun(char *s,char *t)
{
char *p,*r,*a;
/*************found**************/
a=Null;
while(*s)
{ p=s;r=t;
while(*r)
/*************found**************/
if(r= =p) {r++;p++;}
else break;
if(*r=='\0') a=s;
s++;
}
return a;
}
main()
{char s[100],t[100],,*p;
clrscr();
printf("\nPlease enter string S: ");
scanf("%s",s);
printf("\nPlease enter substring t: ");
scanf("%s",t);
p=fun(S,t);
if(p) printf("\nThe result is:%s\n",p);
else printf("\nNot found!\n ");
}
●试题四
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编写程序,其功能是:以行为单位把字符串中所有小写字母o左边的字符串内容移到该串的右边存放,然后把小写字母o删除,余下的字符串内容移到已处理字符串的左边存放。最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT(),把结果XX输出到文件OUT5.DAT中。
例如:原文:You can create an index on any field.
you have the correct record.
结果:n any field.Yu can create an index
rd.yu have the crrect rec
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "ctype.h"
#include "mem.h"
unsigned char xx[50][80];
int maxline=0;
int readdat(void);
void writedat(void);
/*将题目要求的字符串中所有小写字母o左边的字符串内容移到该串的右边存放,即
将串中"最后"一个字母o左右两侧的内容互换*/
void StrOR(void)
{
inti;
char*p1,*p2,t[80];
for(i=0;i<maxline;i++)
{ t[0]=′/0′;
p2=xx[i];
while(*p2)/*找到最后一个′o′*/
{if((1) )p1=p2;
p2++;
}
strcat(t,p1+1);
*p1=′\\0′;
strcat(t,xx[i]);
p1=xx[i];
p2=t;
while(*p2)/*删去字符′o′*/
{if((2) ) (3) =*p2;
p2++;
}
(4) ;
}
}
void main()
{
clrscr();
if(readdat())
{printf("Can't open the file IN.DAT!\\n");
return;
}
StrOR();
writedat();
}
int readdat(void)
{
FILE*fp;
int i=0;
char*p;
if((fp=fopen("in.dat","r"))==NULL)
return 1;
while(fgets(xx[i],80,fp)!=NULL)
{p=strchr(xx[i],′\\n′);
if(p)
*p=0;
i++;
}
maxline= (5) ;
fclose(fp);
return 0;
}
void writedat(void)
{FILE*fp;
int i;
fp=fopen("in.dat","w");
for(i=0;i<maxline;i++)
{printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
}
}
阅读以下说明和C代码,将应填入(n)处的字句写在对应栏内
【说明】
从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编写程序,其功能是:以行为单位把字符串中所有小写字母。左边的字符串内容移到该串的右边存放,然后把小写字母。删除,余下的字符串内容移到已处理字符串的左边存放。最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT(),把结果XX输出到文件 OUT5.DAT中。
例如:原文:You can create an index on any field.
you have the correct record.
结果:n any field.Yu can create an index
rd. yu have the crreet res
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
include "stdio.h"
include "string.h"
include "conio.h"
include "ctype.h"
include "mem.h"
unsigned char xx[50] [80]
int maxline=0;
int readdat(void);
void writedat(void)
/*将题目要求的字符串中所有小写字母o左边的字符串内容移到谊串的右边存放,即将串中“最后”一个字母o左右两侧的内容互换*/
void StrOR(void)
{
inti;
char*p1,* p2,t[80];
for(i=0;i<maxline;i++)
{ t[0]='/0';
p2=xx[i];
while(*p2) /*找到最后一个别'o'*/
{ if((1))p1=p2;
p2++;
}
strcat(t,p1+1);
*p1='\0';
strcat(t,xx[i]);
p1=xx[i];
p2=t;
while(*p2) /*删去字符'o'*/
{ if((2)) (3)=*p2;
p2++;
}
(4);
}
}
void main()
{
clrscr();
if(readdat())
{ printf("Can't open the file IN. DAT!\n");
return;
}
StrOR();
writedat();
}
int readdat(void)
{
FILE * fp;
int i=0;
char * p;
if((fp=fopen("in.dat","r" ))==NULL)
return 1;
while(fgets(xx[i],80, fp)!=NULL)
{ p=strchr(xx[i],'\n');
if(p)
*p=0;
i++;
}
maxline:(5);
fclose(fp);
return 0;
}
void writedat (void)
{ FILE * fp;
int i;
fp=fopen("in.dat',"w");
for(i=0;i<maxline;i++)
{ printf("%s\n",xx[i]);
fprintf(fp," %s\n",xx[i]);
}
fclose(fp);
}
}
}
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!