【问题标题】:Is there any alternative for 'substr()'?'substr()'有什么替代方法吗?
【发布时间】:2021-08-20 22:00:26
【问题描述】:
char patern[]="AGAAGAG";
int n = strlen(patern);
int pat[n];
for (int i = 0; i < n; i++)
{
    int j = 0;
    while(j <= i )
    {
        if (patern.substr(0, j) == patern.substr(i-j+1, j))
        {
            pat[i] = j;
        }
        j++;
    }
    cout<<pat[i]<<endl;
}

我只能使用&lt;iostream&gt;&lt;cstring&gt;&lt;cstdio&gt;&lt;cstdlib&gt;。输出为0011232

【问题讨论】:

  • 我不明白你的问题是什么。
  • char[] 没有方法。你确定你没有使用std::string
  • @zead seam 另一种选择是 C 函数 strncmp。
  • @SilvioMayolo 如果我们用字符串 patern="AGAAGAG" 更改前两行;
  • @VladfromMoscow 谢谢

标签: c++ arrays char string-comparison c-strings


【解决方案1】:

由于字符数组没有像substr 这样的方法,因此另一种方法是使用在标头&lt;cstring&gt; 中声明的C 标准函数strncmp。例如

if ( strncmp( patern, patern + i-j+1, j ) == 0 )

【讨论】:

  • 我不知道该怎么做,你真的很感激它
  • @zeadseam 感谢人们支持并接受他们的回答
猜你喜欢
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多