打大一学校考试C语言过了后,这六七年就没再碰过C语言,今天终于又因为种种原因需要重新学习C,发现概念丢的都差不多了。整了好长时间,终于写成了这个简单的破程序,贴出来,留个念!
/* 入口参数为两个字符串A、B,返回值为整数类型,为B在A中第一次出现的位置,首字符为1,依次排序。不出现返回0,若长度大于A,返回-1. */
int str(char *src,char *dst)
{
int i;
int j;
int k;
int p;
//如果传过来的字符为空,则K、P递增,以此获取字符串A、B的长度
lr_log_message("this is i before");
for (i=0;*(src+i) != '\0'; i++)
{
k++;
// lr_log_message("the length of the String A is:%d",k);
}
lr_log_message("the length of the String A is:%d",k);
lr_log_message("this is i end");
lr_log_message("this is j before");
for (j=0;*(dst+j) != 0; j++)
{
p++;
// lr_log_message("the length of the String B is:%d",p);
}
lr_log_message("the length of the String B is:%d",p);
lr_log_message("this is j end");
//B长度大于A,返回-1
if (p>k) {
return -1;
}
//B长度小于A,开始进行判断,A中第一个出现B的位置
for(k=0;k<=i-j;k++)
{
for(p=0;p <j;p++)
{
if (src[k+p]!=dst[p])
{
break;
}
}
if(p==j) //字符串匹配
{
return k+1;
}
}
}
vuser_init()
{
int z;
// int i,k;
char src[30];
char dst[30];
strcpy(src, "abdcdefggsdfd");
strcpy(dst, "sdfd");
/*
for (i=0;*(src+i) != 0; i++)
{
k++;
lr_log_message("k is:%d",k);
}
*/
z=str(src,dst);
lr_log_message("the first station is %d",z);
return 0;
}