标题:[求助]helpfg救命 1道题 谢谢
只看楼主
superloner
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-12-14
 问题点数:0 回复次数:1 
[求助]helpfg救命 1道题 谢谢
1求子串在主串中的位置或置换子串
给定主串和子串,显示出子串在主串中的第一个位置,其子串在串中不存在,则返0
C语言设计!
搜索更多相关主题的帖子: helpfg 救命 
2005-12-14 14:50
沉默的羔羊1013
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2005-12-10
得分:0 

这个差不多吧,虽说效率低了点。。。
int search(unsigned char *s1,unsigned char *s2)
{
int i,j,position=0;
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]==s2[0]&&position==0)
{
position=i+1;
for(j=1;s2[j]!='\0';j++)
{
if(s1[i+j]!=s2[j])
{
position=0;
}
}
}
}
return position;
}

main()
{
int position;
unsigned char a[50]={"i am chinese,you are not chinses!"};
unsigned char b[20]={"chinese"};
position=search(a,b);
printf("%d\n",position);
getch();
}

2005-12-14 18:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-38124-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.242304 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved