谁能帮帮我做这个C++

来源:百度知道 编辑:UC知道 时间:2024/05/06 07:37:39
CZ1102: Problem Solving and Computation

Tutorial/Lab Assignment 4

Assignment solution is submitted in the same way as the previous ones before Monday (10 November) in Week 13.

Question 1. Write the following functions on strings:

(a) int Suffix_Finding(char *str, int len, char *suffix);

This function copies the suffix of length len of string str into string suffix and returns 1 if len is smaller than or equal to the length of str; and it returns 0 without coping if len is larger than the length of str.

(ii) char *Is_Substring(char *str1, char *str2);

This function checks whether str2 is a substring of str1. If str2 is a substring of str1,
the function returns a pointer pointing to the first occurrence of str2 in str1. It returns NULL if str2 is not a substring of str1.

You test the program with the following main() body.

main(int argc, char *ar

#include"iostream"
using namespace std;
#include"stdio.h"
#include"string.h"
int Suffix_Finding(char *str, int len, char *suffix)//查找后缀
{
int i,j;
if (len>strlen(str))
return 0;
j=strlen(str);
for(i=0;i<=len;i++)
*(suffix+i)=*(str+(j-len)+i);
return 1;

}

char *Is_Substring(char *str1, char *str2)//查询子序列
{
int i,j;
if(strlen(str1)<strlen(str2)) return NULL;
i=0;j=0;
while(i<strlen(str1)&&j<strlen(str2))
if(*(str1+i)==*(str2+j))
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
if(j>=strlen(str2))
return str1+i-strlen(str2);//如果匹配返回str2在str1中第一个字符地址

else
return NULL;
}

int main()
{
char a[10],b[10],c[10];
gets(a);
gets(b);
Suffix_Finding(a, 4, c);//复制a的4个后缀字母给c
printf(