在string1中查找是否出现string2?

liner09136 2006-05-18 11:47:36
#include<stdio.h>
int stringlen(char *s);
int stringcmp(char *s,char *t);
int stringend(char *s,char *t);
int stringin(char *s,char *t);

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

int stringcmp(char *s,char *t){
for( ; *s == *t ; s++,t++){
if(*s == '\0') return 0;
}
return *s-*t;
}

int stringend(char *s,char *t){
int strlens,strlent;
strlens=stringlen(s);
strlent=stringlen(t);
s+=strlens-strlent;
if(stringcmp(s,t)){
return 0;
}
else return 1;
}

int stringin(char *s,char *t){
int strlens,strlent,flag=0;
char *p;
strlens=stringlen(s);
strlent=stringlen(t);
for(p=s;p<=(s+strlens-strlent);p++){
*(p+strlent)='\0';
if(stringcmp(p,t))
flag=0;
else{
flag=1;
break;
}
}
/*
p=s;
*(p+strlent)='\0';
if(stringcmp(p,t))
return 0;
else
return 1;
*/
return flag;
}

int main()
{
int result;
char *str1=(char* )malloc(sizeof(char)*128); /*dynamically alloc memory in heap*/
char *str2=(char* )malloc(sizeof(char)*64);
if(str1==NULL || str2==NULL){
puts("memory alloc fail\n");
exit(1);
}
printf("please input string1:");
gets(str1);
printf("please input string2:");
gets(str2);
result=stringin(str1,str2);
if(result)
printf("string2 is in string1.byebye\n");
else
printf("string2 is not in string1.byebye\n");
free(str1);
free(str2);
str1=NULL;
str2=NULL;
return(0);
}

能不能帮我看一下这个程序,使用stringend( )的时候可以检查string2是不是在string1的尾部,但是使用stingin( )的时候检查不出string2 是不是在string1 里面,请问如何修改stringin( )这个function
谢谢
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhu_doc 2006-05-18
  • 打赏
  • 举报
回复
在string1中查找是否出现string2?
-----------------------------
不用那么复杂:
我用kmp和普通的匹配函数都实现了。
#include <iostream>
#include <string>
using namespace std;

void get_nextval(const char *T, int next[])
{
// 求模式串T的next函数值并存入数组 next。

int j = 0, k = -1;
next[0] = -1;

while ( T[j/*+1*/] != '\0' )
{
if (k == -1 || T[j] == T[k])
{
++j; ++k;
if (T[j]!=T[k])
next[j] = k;
else
next[j] = next[k];
}// if
else
k = next[k];
}// while

for(int i=0;i<j;i++)
{
cout<<"next["<<i<<"] = "<<next[i]<<endl;
}
cout<<endl;

}// get_nextval 


int KMP(const char *Text,const char* Pattern) //const 表示函数内部不会改变这个参数的值。
{
if( !Text||!Pattern|| Pattern[0]=='\0' || Text[0]=='\0' )//
return -1;//空指针或空串,返回-1
int len=0;

const char * c=Pattern;
while(*c++!='\0')//移动指针比移动下标快。
{
++len;//字符串长度.
}
int *next=new int[len+1];
get_nextval(Pattern,next);//求Pattern的next函数值
int index=0,i=0,j=0;
while(Text[i]!='\0' && Pattern[j]!='\0' )
{
if(Text[i]== Pattern[j])
{
++i;// 继续比较后继字符
++j;
}
else
{
index += j-next[j];
if(next[j]!=-1)
j=next[j];// 模式串向右移动
else
{
j=0;
++i;
}
}
}//while
delete []next;

if(Pattern[j]=='\0')
return index+1;// 匹配成功
else
return -1;
}

int main()//abCabCad
{
// char* text="bababCabCadcaabcaababcbaaaabaaacababcaabc";
char *text = "aaaaaab";
char*pattern="aaaab";
//getNext(pattern,n);
//get_nextval(pattern,n);
cout<<text<<endl;
cout<<pattern<<endl;
cout<<KMP(text,pattern)<<endl;
return 0;
}
/* char res[100];
char str[20];

cout<<"input the context to be matched: "<<endl;
cin.getline(res,100,'\n');

cout<<"input the key character:"<<endl;
cin.getline(str,20,'\n');

cout <<res<<endl;
cout <<str<<endl;

int index_=-1;
int count=0;
do
{
index_ = KMP(res, str);
if( -1 == index_ )
cout<<"so now,there is no more matching position"<<endl;
else
{
count++;
cout <<"the "<<count<<" matching characters: "<< index_<<endl;
}
}while(index_ != -1);

return 0;
*/
liner09136 2006-05-18
  • 打赏
  • 举报
回复
分不够,不好意思啊
liner09136 2006-05-18
  • 打赏
  • 举报
回复
呵呵,我就是想自己写一个strstr嘛
那些strlen,strcmp也自己写嘛
谢谢各位,我已经知道怎么写啊
熊主任 2006-05-18
  • 打赏
  • 举报
回复
楼上把我想说的说了~~~
逸学堂 2006-05-18
  • 打赏
  • 举报
回复
用strstr,多好啊。
dch4890164 2006-05-18
  • 打赏
  • 举报
回复
泛型算法find
----------------------
find()
template< class InputIterator, class T >
InputIterator
find( InputIterator first,
InputIterator last, const T &value );
find()利用底层元素类型的等于操作符对[first,last)范围内的元素与value 进行比较当
发现匹配时结束搜索过程且find()返回指向该元素的一个InputIterator 如果没有发现匹
配则返回last
#include <algorithm>
#include <iostream.h>
#include <list>
#include <string>
int main()
{
int array[ 17 ] = { 7,3,3,7,6,5,8,7,2,1,3,8,7,3,8,4,3 };
int elem = array[ 9 ];
int *found_it;
found_it = find( &array[0], &array[17], elem );// 结果: find the first occurrence of 1 found!
cout << "find the first occurrence of "
<< elem < "\t"
<< ( found_it ? "found!\n" : "not found!\n" );
string beethoven[] = {
"Sonata31", "Sonata32", "Quartet14", "Quartet15",
"Archduke", "Symphony7" };
string s_elem( beethoven[ 1 ] );
list< string, allocator > slist( beethoven, beethoven+6 );
list< string, allocator >::iterator iter;
iter = find( slist.begin(), slist.end(), s_elem );
// 结果: find the first occurrence of Sonata32 found!
cout << "find the first occurrence of "
<< s_elem < "\t"
<< ( iter != slist.end() ? "found!\n" : "not found!\n" );
//来自c++ primer
summerCsdn 2006-05-18
  • 打赏
  • 举报
回复
不是有现成的函数吗,strstr,strrstr

69,382

社区成员

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

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