C++实训题目

2025-03-14 19:21:37
推荐回答(1个)
回答1:

完全按你要求写的
#include
#include
using namespace std;

char* trim(char *s);
char* leftstring(char *s1, char *s2, int n);
int index(char *s1, char *s2);

int main(){
char str1[]="I'm student. ",str2[]="student",str3[4];
int n;
cout<<"串str1:"< trim(str1);
cout<<"串str1:"< leftstring(str1,str3,3);
cout<<"串str3:"< cout<<"串str2是:"< n=index(str1,str2);
if(n!=-1) cout<<"串str1包含子串str2,从第"< else cout<<"串str1不包含子串str2"< return 0;
}
char* trim(char *s){
int i=0;
while(*(s+i)!='\0') i++;
i--;
while(*(s+i)==' ') *(s+i--)='\0';
return s;
}
char* leftstring(char *s1, char *s2, int n){
int i;
for(i=0;i *s2='\0';
return s2;
}
int index(char *s1, char *s2){
bool b=0;
int i,j,n=-1,n1=strlen(s1),n2=strlen(s2);
for(i=0;i if(*(s1+i)==*s2){
b=1;
for(j=0;j if(*(s1+i+j)!=*(s2+j)){
b=0;
break;
}
}
if(b==1) {n=i;break;}
}
}
return n;
}