字符窜子串查找

starcat 2009-07-19 04:45:19
求简单清晰的源代码,我的太混乱了。

题目是:查找子串在长字符窜中最右边第一次出现的位置,如果没有就返回-1
(Write the function strindex(s,t) which returns the position of the rightmost occurrence of t in s, or -1 if there is none.)

...全文
641 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
SSSummering114 2009-07-20
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>

const int M=100;
const int N=30;

void main()
{
char longstr[M];
char substr[N];
int i;
int last=-1;

printf("Input a long string:");
scanf("%s",longstr);
printf("Input a target substring:");
scanf("%s",substr);

for(i=0;i<strlen(longstr);i++)
{
if(longstr[i]==substr[0])
{
if(strncmp(&longstr[i],substr,strlen(substr))==0) last=i+1;
}
}

if(last==-1){printf("Can not find the target substring!\n");}
else {printf("The last occurance of the substring is at the number %d character of the longstring!\n",last);}
}

学习 ~~~
SSSummering114 2009-07-20
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>

const int M=100;
const int N=30;

void main()
{
char longstr[M];
char substr[N];
int i;
int last=-1;

printf("Input a long string:");
scanf("%s",longstr);
printf("Input a target substring:");
scanf("%s",substr);

for(i=0;i<strlen(longstr);i++)
{
if(longstr[i]==substr[0])
{
if(strncmp(&longstr[i],substr,strlen(substr))==0) last=i+1;
}
}

if(last==-1){printf("Can not find the target substring!\n");}
else {printf("The last occurance of the substring is at the number %d character of the longstring!\n",last);}
}

学习~~~
liushac 2009-07-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

#define MAXCHAR 1000 /* 最大输入字符 */

int strrindex(char s[],char searchfor[]);
int inputlines(char s[],int lim);

char t[]="ould"; /* 要查找的模式,可修改测试 */

/* 字符串t在s中最后出现的位置,如果s中不包含t,那么返回-1 */
int main()
{
int i=0,len=0,found=0;
char s[MAXCHAR];
for (i=0;i<MAXCHAR;i++)
{
s[i]=0;
}

printf ("input s: (end input on windows:enter,ctrl+z;on linux :enter,ctrl+d)\n");
len=inputlines(s,MAXCHAR);
found=strrindex(s,t);
printf ("find \"%s\" in s,(place begin 0) place is: %d\n",t,found);

return found;
}

/* strrindex函数: 返回字符串t在s中最后出现的位置,如果s中不包含t,那么返回-1。*/
int strrindex(char s[],char t[])
{
int i=0,j=0,place=0;
i=strlen(s);
i--;
while (s[i]!='\0' && i>=0 && place==0)
{ /* 从后往前读取s中的元素 */
j=0;
while (s[i]==t[j] && t[j]!='\0' && s[i]!='\0')
{ /* s中找到t中第一个元素后,s反向变成从前往后比对t中的元素 */
i++;
j++;
}
if (t[j]=='\0')
{ /* t为空或 在s中包含t*/
place=i-j;
}
else
{ /* s中不包含t */
i-=j;
}

i--;
}
if (place==0)
{
place=-1;
}
return place;
}
/* inputlines函数:保存字符数组,返回字符串长度 */
int inputlines(char s[],int lim)
{
int i=0;
int c;
while (i<lim-1 && (c=getchar())!=EOF)
{
s[i]=c;
i++;
}
s[i]='\0';

return i;
}
yingkundu 2009-07-20
  • 打赏
  • 举报
回复
应该加上条件:允许用或者禁止用库函数!
如果允许用库函数的话,那很简单。
如果禁止用库函数的话,那就麻烦了。

谁能不用任何库函数写出来?
gods_word 2009-07-19
  • 打赏
  • 举报
回复
推荐使用C++ STL库中的查找函数。
licry01 2009-07-19
  • 打赏
  • 举报
回复
其实不难啊, 就是要从源字串的右边开始查找,找到就立即结束, 效率要比从左边开始找要高。



int strindex(const char * source_buff, const char * searchfor_buff)
{
int pos = (-1) ;
unsigned int source_buff_size = 0, searchfor_buff_size = 0, p = 0;

source_buff_size = strlen(source_buff) * sizeof(char) ;
searchfor_buff_size = strlen(searchfor_buff) * sizeof(char) ;

if(searchfor_buff_size > source_buff_size) { return pos ; }

p = (unsigned int)source_buff + source_buff_size - searchfor_buff_size ;

while(p >= (unsigned int)source_buff )
{
//如果大小写不敏感用memicmp
if( memcmp((void*)p, searchfor_buff, searchfor_buff_size) == 0)
{
pos = p - (unsigned int)source_buff;
break;
}
p--;
}
return pos;
}


billow_zhang 2009-07-19
  • 打赏
  • 举报
回复
不好意思,上面的程序是找到最左边的位置,按照原题意,应该是最右边的字串位置.这有些难度.
程序如下:

int
strindex(const char *s, const char *t)
{
const char *ps = s;
const char *pt = t;

while( *ps != '\0' )
ps++;

while( *pt != '\0' )
pt++;

pt--;

while ( --ps >= s ) {
const char *pps = ps;
const char *ppt = pt;

while( *pps-- == *ppt-- ) {
if ( ppt < t )
return pps - s + 1;
if ( pps < s )
return -1;
}
}

return -1;
}
billow_zhang 2009-07-19
  • 打赏
  • 举报
回复
写这样的函数会锻炼使用C的能力.楼主的程序确实不太简炼.在这样的本身就是基础的函数中,是忌讳再使用函数调用的.楼主一开始就使用了两个strlen就让我看不下去了.下面的格式太乱,需要注意程序风格.我下面给出我的程序供参考:


int strindex(const char *s, const char *t)
{
const char *ps = s;

while( *ps != '\0' ) {
const char *pps = ps;
const char *pt = t;

while( *pps++ == *pt++ ) {
if ( *pt == '\0' )
return ps - s;
}

ps++;
}

return -1;
}
过去的我 2009-07-19
  • 打赏
  • 举报
回复
char *nfind(char *str,char *pat)
{
char *p1=str;
char *p2=pat;
while (*p1)
{
if( *p1== *p2)
{
int i;
int bfind=1;
for(i=1;i<strlen(pat);i++)
{
if( p1[i] !=p2[i])
{
bfind =0;
continue;
}
}
if (bfind == 1)
return p1;


}

p1++;
}

}
wisaa 2009-07-19
  • 打赏
  • 举报
回复
学习 !!
大前置 2009-07-19
  • 打赏
  • 举报
回复
直接用C++现成函数会好得多
jn989 2009-07-19
  • 打赏
  • 举报
回复
C++ STL库中的字符串查找函数:
* find:查找;
* rfind:反向查找;
* find_first_of:查找包含子串中的任何字符,返回第一个位置;
* find_first_not_of:查找不包含子串中的任何字符,返回第一个位置;
* find_last_of:查找包含子串中的任何字符,返回最后一个位置;
* find_last_not_of:查找不包含子串中的任何字符,返回最后一个位置。
用这些现有的库函数,自己再组织一下,应该会更简单些
starcat 2009-07-19
  • 打赏
  • 举报
回复
沙发自己坐

My source file:

#include <stdio.h>
#include <string.h>
int strindex(char [], char []);

char pattern[] = "ould";

main()
{
char line[1000], c;
int found;
gets(line);
found = strindex(line, pattern);
if(found + 1)
printf("Found! in position %d\n", found);
else
printf("Not found!\n");
getch();

}

int strindex(char source[], char searchfor[])
{
int j, k, l;
j = strlen(source) - 1;
l = k = strlen(searchfor);
while(j >= -1)
{
int count = 0;
while(source[j] == searchfor[k - 1] && k > -1)
{
--j;
--k;
++count;
}

if(k == 0)
return j + 1;
else
{
k = l;
while(count--)
++j;
}
--j;

}
return -1;
}


69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧