请编写一个函数 int find(char s[],char t[]), 该函数在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置(整数值):否则返回-1。本题要求:用数组方式及两重循环来实现该函数。注意:部分源程序已存在考生文件夹的文件PROC1.cpp中。请勿修改主函数和其他函数中的任何内容,仅在函数find()的花括号中填写若干语句。文件PROC1.cpp的内容如下://PROC1.cppinclude<iostream>using namespace std;int find(char s[],char t[]);const int MAXLINE = 256;int main(){char source[MAXLINE],target[MAXLINE];cout<<"Please input a string for searching:\n";cin.getline(source,MAXLINE);cout<<"Please input a string you want to find:\n";cin.getline(target,MAXLINE);int intPos=find(source,target);if(intPos>=0)cout<<"Finding it,The target string is at index"<<intPos<<"of the source string\n";elsecout<<"Not finding it \n";return 0;}int find(char s[],char t[]){//********}

请编写一个函数 int find(char s[],char t[]), 该函数在字符串s中查找字符串t,如果找到,则返回字符串t在字符串s中的位置(整数值):否则返回-1。本题要求:用数组方式及两重循环来实现该函数。

注意:部分源程序已存在考生文件夹的文件PROC1.cpp中。

请勿修改主函数和其他函数中的任何内容,仅在函数find()的花括号中填写若干语句。

文件PROC1.cpp的内容如下:

//PROC1.cpp

include<iostream>

using namespace std;

int find(char s[],char t[]);

const int MAXLINE = 256;

int main()

{

char source[MAXLINE],target[MAXLINE];

cout<<"Please input a string for searching:\n";

cin.getline(source,MAXLINE);

cout<<"Please input a string you want to find:\n";

cin.getline(target,MAXLINE);

int intPos=find(source,target);

if(intPos>=0)

cout<<"Finding it,The target string is at index"

<<intPos<<"of the source string\n";

else

cout<<"Not finding it \n";

return 0;

}

int find(char s[],char t[])

{

//********

}


相关考题:

函数my_cmp()的功能是比较字符串s和t的大小,当s等于t时返回0,否则返回s和t的第一个不同字符的ASCII码差值,即s t时返回正值,当s t时返回负值。请填空。my_cmp(char *s, char *t){while (*s == *t){if (*s == ′\0′)return 0;++s; ++t;} return 【18】 ;}

函数sstrcmp()的功能是对两个字符串进行比较。当s所指字符串和t所指字符串相等时,返回值为0;当s所指字符串大于t所指字符串时,返回值大于0;当s所指字符串小于t所指字符串时,返回值小于0(功能等同于库函数strcmp()),请填空。#includestdio.hint sstrcmp(char *s,char *t){ while(*s*t*s= =){s++;t++; }return;}

[说明1]函数void convelt(chal *a,int n)是用递归方法将一个正整数n按逆序存放到一个字符数组a中,例如,n=123,在a中的存放为'3'、'2'、'1'。[C函数1]void convert(char *a,int n){ int i;if((i=n/10)!=0; convert( (1) ,i);*a= (2) ;}[说明2]函数int index(char *s,char *t)检查字符串s中是否包含字符串t,若包含,则返回t在s中的开始位置(下标值),否则返回-1。[C函数2]int index(char *s,char *t){ int i,j=0;k=0;for(i=0;s[i]!:'\0';i++)( for( (3) ;(t[k]!='\0')(s[j]!='\0')( (4) );j++,k++);if( (5) ) return(i);}return(-1);}

编写算法,实现下面函数的功能。函数void insert(char*s,char*t,int pos)将字符串t插入到字符串s中,插入位置为pos。假设分配给字符串s的空间足够让字符串t插入。(说明:不得使用任何库函数)

下列给定程序中函数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 ");}

下列给定程序中,函数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);}

函数mycmp(char *s,char *t)的功能是比较字符串s和t的大小,当s等于t时返回0,当st时返回正值,当st时返回负值,请填空。mycmp( char *s,char *t){ while (*s==*t) { if (*s==’\0’)return 0; ++s;++t; } return();}

有以下函数: int fun(char * s) {char * t=s; while(*t++); return(t-s) 该函数的功能是______。A.比较两个字符串的大小B.计算s所指字符串占用内存字节个数C.计算s所指字符串的长度D.将s所指字符串复制到字符串t中

函数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 【 】;}

(选做题)查找字符串。输入两个字符串s和t,在字符串 s中查找子串t,输出起始位置,若不存在则输出-1。要求自定义函数char *search(char *s,char *t)返回子串t的首地址,若未找到,则返回NULL。