C++ 在较大字符串中查找较小字符串

C++ 在较大字符串中查找较小字符串,c++,C++,指令是:您的程序从用户处接收两个符号字符串:首先是长符号,然后是短符号。然后,程序返回长字符串中的所有位置,其中短字符串作为子字符串出现。 例如:长字符串是“aabcccbabc”,短字符串是“abc”。在位置1和7的长字符串“aabcccbabc”中可以看到两次短字符串(字符串中的位置枚举始终从0开始)。子字符串的位置是其第一个符号在整个字符串中的位置。不允许使用任何内置工具,如strlen()。 我无法让它输出事件数,也无法输出位置数。请帮忙!我觉得我什么都试过了 #include <

指令是:您的程序从用户处接收两个符号字符串:首先是长符号,然后是短符号。然后,程序返回长字符串中的所有位置,其中短字符串作为子字符串出现。 例如:长字符串是“aabcccbabc”,短字符串是“abc”。在位置1和7的长字符串“aabcccbabc”中可以看到两次短字符串(字符串中的位置枚举始终从0开始)。子字符串的位置是其第一个符号在整个字符串中的位置。不允许使用任何内置工具,如strlen()。 我无法让它输出事件数,也无法输出位置数。请帮忙!我觉得我什么都试过了

#include <iostream>
using namespace std;

int main()
{
int NUM_MTCH, POSS1, POSS2, LENS1, LENS2 ;
NUM_MTCH=0;    //NUM_MTCH= THE NUMBER OF MATCHES
POSS1=0;       //POSITION OF PATTERM IN STRING2
POSS2=0;       //POSITION OF PATTERN IN STRING1
LENS1=0;       //LENGTH OF FIRST STRING
LENS2=0;       //LENGTH OF SECOND STRING
bool SUCCESS(true);
string S1, S2;
do
{
    cout<< "Enter First String: ";
    cin>> S1;
    for (POSS1=0; S1[POSS1] != '\0'; POSS1++)
    {
        LENS1++;
    }
    cout<< "Enter Second String: ";
    cin>>S2;
    for (POSS2=0; S2[POSS2] != '\0'; POSS2++)
    {
        LENS2++;
    }

}
while(LENS2>LENS1);

while (S1[POSS1] != '\0')
{
for(POSS1=0; S1[POSS2+POSS1]==S2[POSS2]; POSS2++)
{
    if (S1[POSS1+POSS2] == S2[POSS2])
    {
        SUCCESS==true;
    }
}
}

while (S1[POSS1]!= '\0')
{
if (S1[LENS2]==S2[POSS2])
{
    NUM_MTCH++;
    if(NUM_MTCH != 0)
    {
        cout<<"POSITIONS: "<<POSS1;
        cout<<"NUMBER OF OCCURENCES: "<<NUM_MTCH;
    }
}
}
POSS1++; 
if (NUM_MTCH==0)
{
cout<<"NOTHING";
}

return 0;
}
#包括
使用名称空间std;
int main()
{
int NUM_MTCH,POSS1,POSS2,LENS1,LENS2;
NUM\u MTCH=0;//NUM\u MTCH=匹配数
POSS1=0;//模式在STRING2中的位置
POSS2=0;//模式在STRING1中的位置
LENS1=0;//第一个字符串的长度
LENS2=0;//第二个字符串的长度
成功(真);
字符串S1、S2;
做
{
cout>S1;
对于(POSS1=0;S1[POSS1]!='\0';POSS1++)
{
LENS1++;
}
cout>S2;
for(POSS2=0;S2[POSS2]!='\0';POSS2++)
{
LENS2++;
}
}
而(LENS2>LENS1);
而(S1[POSS1]!='\0')
{
对于(POSS1=0;S1[POSS2+POSS1]==S2[POSS2];POSS2++)
{
如果(S1[POSS1+POSS2]==S2[POSS2])
{
成功=正确;
}
}
}
而(S1[POSS1]!='\0')
{
if(S1[LENS2]==S2[POSS2])
{
NUM_MTCH++;
如果(NUM_MTCH!=0)
{

cout您可以使用来轻松解决此问题。
find()
将搜索字符串以查找所提供字符串的第一个匹配项,并指向找到该字符串的位置,如果未找到任何内容,则返回
std::string::npos
。您可以继续调用
find()
从找到的最后一个位置开始,直到再也找不到子字符串为止

std::string mainString = "aabcccbabc";
std::string subString = "abc";

std::vector<size_t> positions;
std::size_t index = 0;

// find sub-string and add positrion to vector.  If no string found end the loop
while((index = mainString.find(subString, index)) != std::string::npos)
{
    positions.push_back(index);
    ++index;  // increment index to search for next string
}

for (auto e : positions)
    std::cout << e << std::endl;
编辑:

<>因为你有一个糟糕的教练,你不允许使用实际的C++,你可以用以下函数来查找子字符串是否存在:

bool stringcmp(const char* sub, const char* main)
{
    while(*sub && (*sub==*main))
        sub++,main++;
    return *sub == '\0'; // if we didn't reach the end of sub then its false
}
您的主程序如下所示:

char* mainString = "aabcccbabc";
char* subString = "abc";
char* crwaler = mainString;

int positions[100];  // assuming there wont be more than 100 occurrences You might want to do something different
int index = 0;
int occurrences = 0;

// go untill the end of the string
while(*crwaler != '\0')
{
    if (stringcmp(subString, crwaler) == true)
    {
        positions[occurrences] = crwaler - mainString;
        occurrences++;
    }
    crwaler++;
}

for (int i = 0; i < occurrences; i++)
    std::cout <<positions[i] << std::endl;
char*mainString=“aabcccbabc”;
char*subString=“abc”;
char*crwaler=mainString;
int positions[100];//假设出现的次数不超过100次,您可能希望执行不同的操作
int指数=0;
int=0;
//一直走到绳子的末端
而(*crwaler!='\0')
{
if(stringcmp(子字符串,crwaler)==true)
{
位置[出现]=crwaler-主环;
事件++;
}
crwaler++;
}
对于(int i=0;i<出现次数;i++)

这个代码有很大的错误

  • std::string
    不以
    \0
  • 所有大写字母中的格式都很难阅读
  • 到处都是漂浮的环
  • 执行此操作的基本O(n*m)算法(n是字符串1的长度,m是字符串2的长度)如下所示:

    • 对于字符串1(长字符串)中的每个字符
      • 查看此字符是否以字符串1中与字符串2(短字符串)匹配的子字符串开头
    一些简短的代码:

    size_t matches = 0;
    std::string s1 = "aabcccbabc";
    std::string s2 = "abc";
    for(size_t i=0; i < (s1.size()-s2.size()+1); ++i)
    {
        if(s1.substr(i,s2.size()) == s2)
        {
            std::cout << "Found match at index " << i << " in 1st string\n";
            ++matches;
        }
    }
    
    std::cout << "Found " << matches << " match(es)" << std::endl;
    
    变成:

    bool match = true;
    for(size_t j=0;j<s2.size();++j)
    {
        if (s1[i+j] != s2[j])
        {
            match = false;
            break;
        }
    }
    if (match)
    {
        std::cout << "Found match at index " << i << " in 1st string\n";
        ++matches;
    }
    
    bool match=true;
    
    对于(size_t j=0;j嗯,我刚刚对您的代码做了一些更改,现在可以正常工作了,因为您不想使用内置函数作为std::string::size()或std::string::substr()

    但首先要记住: 1-std::字符串未以NUL结尾,因此您对\0的检查将无法工作,如上所述。 2-正如@andyG所说,你所有大写字母的格式很难理解

    这是密码

        #include <iostream>  
        #include<string>
        using namespace std;
    
        int main()
        {
          int NUM_MTCH, POSS1, POSS2, LENS1, LENS2;
          NUM_MTCH = 0;    //NUM_MTCH= THE NUMBER OF MATCHES
          POSS1 = 0;       //POSITION OF PATTERM IN STRING2
          POSS2 = 0;       //POSITION OF PATTERN IN STRING1
          LENS1 = 0;       //LENGTH OF FIRST STRING
          LENS2 = 0;       //LENGTH OF SECOND STRING
          bool SUCCESS(false);
          string S1, S2;
    
          cout << "Enter First String: ";
          cin >> S1;
          S1 += "\0"; 
          for (POSS1 = 0; S1[POSS1] != '\0'; POSS1++)
          {
             LENS1++;
          }
    
          cout << "Enter Second String: ";
          cin >> S2;
          S2 += "\0";
          for (POSS2 = 0; S2[POSS2] != '\0'; POSS2++)
          {
            LENS2++;
          }
    
    
          for (POSS1 = 0; POSS1 < (LENS1 - LENS2 + 1); POSS1++)
          {
              if (S1[POSS1] == S2[0])
              {
                 POSS1++;
                 for (int j = 1; j < LENS2; j++){
                    if (S1[POSS1] == S2[j]){
                       SUCCESS = true;
                       POSS1++;
                    }
                    else{
                       SUCCESS = false;
                    } 
                }
                if (SUCCESS){
                    cout << "at position" << POSS1 - LENS2 << endl;
                    NUM_MTCH++;
                }
                POSS1--;
    
            }
        }
    
    
    
    if (NUM_MTCH == 0)
    {
        cout << "NOTHING";
    }
    else{
        cout <<"number of matches: "<< NUM_MTCH << endl;
    }
    
    
    
    return 0;
    
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    int NUM_MTCH,POSS1,POSS2,LENS1,LENS2;
    NUM\u MTCH=0;//NUM\u MTCH=匹配数
    POSS1=0;//模式在STRING2中的位置
    POSS2=0;//模式在STRING1中的位置
    LENS1=0;//第一个字符串的长度
    LENS2=0;//第二个字符串的长度
    布尔成功(假);
    字符串S1、S2;
    cout>S1;
    S1+=“\0”;
    对于(POSS1=0;S1[POSS1]!='\0';POSS1++)
    {
    LENS1++;
    }
    cout>S2;
    S2+=“\0”;
    for(POSS2=0;S2[POSS2]!='\0';POSS2++)
    {
    LENS2++;
    }
    对于(POSS1=0;POSS1<(LENS1-LENS2+1);POSS1++)
    {
    if(S1[POSS1]==S2[0])
    {
    POSS1++;
    对于(int j=1;j无法输出什么?您尝试过如何调试它?您能描述一下您的尝试吗?很难从文件中为您编写的代码中区分您的尝试。
    std::string
    没有NUL终止,因此您对
    \0
    的检查现在无法工作,它只输出“NOTHING”在我输入两个字符串之后。当我运行程序时,我插入:abghab(第一个字符串);然后我插入ab(第二个字符串)在那之后,我收到了一条信息:没什么我想准确地使用这个,但是我的教授说他不想让我们使用任何内置工具,他想让我们使用所有的循环并拖出代码。@dfg221但是你的老师使用了
    std::string
    ,这是一个“内置工具”。使用
    std::string
    的目的是使用这个类提供的“内置工具”,即公共接口。这是一个多么奇怪的要求。听起来像“洗衣机,但不要启动它——用水填充,用手洗”。@ DFG221你在上什么课?C++编程201:(C++)????dfg221,讽刺的是如果你不知道如何使用“内置工具”,你就找不到C++的工作。@ dfg221如果一个循环是你能使用的,那么唯一的方法就是在这里。如果你不能使用。
    bool match = true;
    for(size_t j=0;j<s2.size();++j)
    {
        if (s1[i+j] != s2[j])
        {
            match = false;
            break;
        }
    }
    if (match)
    {
        std::cout << "Found match at index " << i << " in 1st string\n";
        ++matches;
    }
    
        #include <iostream>  
        #include<string>
        using namespace std;
    
        int main()
        {
          int NUM_MTCH, POSS1, POSS2, LENS1, LENS2;
          NUM_MTCH = 0;    //NUM_MTCH= THE NUMBER OF MATCHES
          POSS1 = 0;       //POSITION OF PATTERM IN STRING2
          POSS2 = 0;       //POSITION OF PATTERN IN STRING1
          LENS1 = 0;       //LENGTH OF FIRST STRING
          LENS2 = 0;       //LENGTH OF SECOND STRING
          bool SUCCESS(false);
          string S1, S2;
    
          cout << "Enter First String: ";
          cin >> S1;
          S1 += "\0"; 
          for (POSS1 = 0; S1[POSS1] != '\0'; POSS1++)
          {
             LENS1++;
          }
    
          cout << "Enter Second String: ";
          cin >> S2;
          S2 += "\0";
          for (POSS2 = 0; S2[POSS2] != '\0'; POSS2++)
          {
            LENS2++;
          }
    
    
          for (POSS1 = 0; POSS1 < (LENS1 - LENS2 + 1); POSS1++)
          {
              if (S1[POSS1] == S2[0])
              {
                 POSS1++;
                 for (int j = 1; j < LENS2; j++){
                    if (S1[POSS1] == S2[j]){
                       SUCCESS = true;
                       POSS1++;
                    }
                    else{
                       SUCCESS = false;
                    } 
                }
                if (SUCCESS){
                    cout << "at position" << POSS1 - LENS2 << endl;
                    NUM_MTCH++;
                }
                POSS1--;
    
            }
        }
    
    
    
    if (NUM_MTCH == 0)
    {
        cout << "NOTHING";
    }
    else{
        cout <<"number of matches: "<< NUM_MTCH << endl;
    }
    
    
    
    return 0;