C程序设计语言(第二版) 5-4 strend(s,t)

strend(s,t) 如果字符串t出现在字符串s的尾部,该函数返回0,否则返回1

 

#include<stdio.h>

int strend(char *s,char *t);

int main(){


		char *s="hewawowa";
		char *t="wo";
		int cmp=strend(s,t);
		printf("%d\n",cmp);
		return 0;
}

int strend(char *s, char *t)
{
	char *ss =s;//保存s的开头
	char *tt =t;//保存t的开头

	int count=0;
	for(;*s!='\0';s++)
	{
		ss=s;
		for(;*s!='\0'&&*t!='\0'&&*s==*t;s++,t++)
			count++;
		if(*s=='\0'&&*t=='\0')
			return 1;
		t=tt;
		s=ss;
	}
	return 0;
}

 另一个版本的:

 

#include<stdio.h>

int strend(char *s,char *t);
int strlen(char *s);

int main(){
		char *s="hewawoe";
		char *t="wo";
		int result=strend(s,t);
		if(result==0)
			printf("%d\n",1);
		else 
			printf("%d\n",0);
		return 0;

}
int strcmp(char *s, char *t)
{
	while(*s++==*t++)
		;
	if(*s=='\0'||*t=='\0')
		return 0;
	else 
		return *s-*t;
}
int strend(char *s, char *t)
{
	int lens;
	int lent;
	lens = strlen(s);
	lent = strlen(t);
	s=s+(lens-lent);
	int cmp =strcmp(s,t);
	return cmp;
}

int strlen(char *s)
{
	int len=0;
	while(*s++!='\0')
		len++;
	return len;
}



 

你可能感兴趣的:(C++,c,C#,D语言)