首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

用数组如何实现在一个字符串中找到它的子串

2012-04-04 
用数组怎么实现在一个字符串中找到它的子串让下列代码输出two three four five#includeiostream#include

用数组怎么实现在一个字符串中找到它的子串
让下列代码输出two three four five 
#include<iostream>
#include<string.h>
using namespace std;
char GetSubstr(char sub[],char str[]);
void main()
{
char substr[20];
substr[20]=GetSubstr("two","one two three four five");

cout<<"找到的子串为:"<<substr<<endl;
}
char GetSubstr(char sub[],char str[])  
{
int i=0,j=0;
for(j=0;;j++)  

{ if(sub[i]==str[j]) //如果相同,判断sub[i+1]与str[j+1]相同,(1)相同的话再判断sub[i+2]与  
代码不会写??? //str[j+2]是 否相同,(2)不同的话继续寻找与sub[i]相同的元素,直到找到第一
  //完整的第一个字符串








}  


我的想法是将第二个字符串的每一个字符先于第一个字符串的的第一个字符比较,找到相同的,
然后再将第二个字符串的相同字符串后的字符串一个一个再与第一个字符串中的第二个字符相比较,
以此类推直到第一个字符串结束,然后再输出相同字符以及以后的字符。


[解决办法]

C/C++ code
char GetSubstr(char sub[],char str[]);char substr[20];substr[20]=GetSubstr("two","one two three four five");
[解决办法]
这个,C和C++都有现成的函数的,C的是strstr,C++可用string的find
参考下BC的strstr.c源码

C/C++ code
/*-----------------------------------* * filename - strstr.c * * function(s) *        strstr - scans a string for the occurrence of a given string *        wcsstr - scans a wide-character string for the occurrence of a *        given wide-characterstring *-----------------------------------*//* *      C/C++ Run Time Library - Version 11.0 * *      Copyright (c) 1987, 2002 by Borland Software Corporation *      All Rights Reserved. * *//* $Revision: 9.8.2.1 $        */#include <string.h>#include <tchar.h>/*-----------------------------------*Name            strstr, wcsstr - scans a string for the occurrence of a                                 given string.Usage           char *strstr(const char *str1, const char *str2);        wchar_t *strstr(const wchar_t *str1, const wchar_t *str2);Prototype in    string.hDescription     strstr and wcsstr scan str1 for the first occurrence of the                substring str2.Return value    strstr and wcsstr return a pointer to the element in str1                that contains str2 (points to str2 in str1). If str2 does                not occur in str1, strstr returns NULL.*------------------------------------*//* This routine works better without any extra optimization at this point */#pragma option push -Od -r_TCHAR * _RTLENTRY _EXPFUNC _tcsstr(const _TCHAR *str1, const _TCHAR *str2){    _TCHAR *c1, *c2, *s = (_TCHAR *)str1;    if (!*str2)        return (_TCHAR *)str1;    /* return str1 if str2 is empty */    if (!*str1)        return NULL;              /* return NULL if str1 is empty */    while(*s)    {        if (*s == *str2)          /* wait for the first matching char */        {            c1 = s;            c2 = (_TCHAR*)str2;                                  /* continue matching chars in the sub-str */            while (*c1 && *c2 && (!(*c2 - *c1)))            {                c2++;                c1++;            }            if (!*c2)             /* if we've run off the end of str2 */                return s;         /* then we've found the first substr match */        }        s++;                      /* otherwise try the next char */    }    return NULL;                  /* didn't find anything */}#pragma option pop 

热点排行