【问题标题】:Reverse Find pointer to nth occurrence of a character in cstring反向查找指向 cstring 中第 n 次出现的字符的指针
【发布时间】:2015-05-14 14:02:56
【问题描述】:

如何反向查找指向 cstring/BSTR 中第 n 次出现的字符的指针?

char * RFindNthOccurrence(char* src, char t, int n)
{
   //for i/p string src = "HI,There,you,All"
   // and t =','
   // n =2
   //returned pointer should be at ",you,All" in same unchanged string
}

我已经找到了第一次和最后一次出现的搜索,但是没有修改字符串反向查找第 n 次出现是问题。

【问题讨论】:

  • 找到第一次出现的代码是什么?您应该只需要为其添加一个计数器以继续运行,直到您看到第 n 次出现。
  • 找到字符串的结尾,一边倒一边数数。
  • 您是要反向查找从开头开始的第 n 次出现还是从结尾开始的第 n 次出现?

标签: c++ string bstr


【解决方案1】:

这个怎么样

// assume n > 0
char * RFindNthOccurrence(char * str, char t, int n) {
    int count = 0;
    char *res = NULL;
    while (str) {
       if (t == *str){
        ++count; 
        if (count >= n) {
            res = str;
       }
       ++str;
    }
    return res;
}

【讨论】:

    【解决方案2】:

    这个怎么样?

    #include <iostream>
    
    const char * RFindNthOccurrence( const char *s, char c, size_t n )
    {
        if ( !n ) return NULL;
    
        while ( ( n -= *s == c ) && *s ) ++s;
    
        return n == 0 ? s : NULL;
    }
    
    char * RFindNthOccurrence( char *s, char c, size_t n )
    {
        if ( !n ) return NULL;
    
        while ( ( n -= *s == c ) && *s ) ++s;
    
        return n == 0 ? s : NULL;
    }
    
    int main() 
    {
        const char *s1 = "HI,There,you,All";
    
        std::cout << RFindNthOccurrence( s1, ',', 2 ) << std::endl;
    
        char s2[] = "HI,There,you,All";
    
        std::cout << RFindNthOccurrence( s2, ',', 2 ) << std::endl;
    
        return 0;
    }
    

    程序输出是

    ,you,All
    ,you,All
    

    该函数的行为方式与标准 C 函数 strchr 相同,即它会找到终止零字符,但仅在 n = 1 的情况下。

    另一个例子

    #include <iostream>
    
    const char * RFindNthOccurrence( const char *s, char c, size_t n )
    {
        if ( !n ) return NULL;
    
        while ( ( n -= *s == c ) && *s ) ++s;
    
        return n == 0 ? s : NULL;
    }
    
    char * RFindNthOccurrence( char *s, char c, size_t n )
    {
        if ( !n ) return NULL;
    
        while ( ( n -= *s == c ) && *s ) ++s;
    
        return n == 0 ? s : NULL;
    }
    
    int main() 
    {
        const char *s = "HI,There,you,All";
        const char *p = s;
    
        for ( size_t i = 1; p = RFindNthOccurrence( s, ',', i ); ++i )
        {
            std::cout << i << ": " << p << std::endl;
        }
    
        return 0;
    }
    

    程序输出是

    1: ,There,you,All
    2: ,you,All
    3: ,All
    

    您可以使用标准 C 函数 strchr 执行相同的操作,而无需编写特殊函数。例如

    #include <iostream>
    #include <cstring>
    
    int main() 
    {
        const char *s = "HI,There,you,All";
        const char *p = s;
        size_t n = 2;
    
        while ( ( p = std::strchr( p, ',' ) ) && --n ) ++p;
    
        if ( n == 0 ) std::cout << p << std::endl;
    
        return 0;
    }
    

    程序输出是

    ,you,All
    

    如果您确实需要反向搜索,那么该功能可以看起来像这个演示程序中的样子

    #include <iostream>
    #include <cstring>
    
    const char * RFindNthOccurrence( const char *s, char c, size_t n )
    {
        if ( !n ) return NULL;
    
        const char *p = s + std::strlen( s );
    
        while ( ( n -= *p == c ) && p != s ) --p;
    
        return n == 0 ? p : NULL;
    }
    
    int main() 
    {
        const char *s = "HI,There,you,All";
        const char *p = s;
    
        for ( size_t i = 1; p = RFindNthOccurrence( s, ',', i ); ++i )
        {
            std::cout << i << ": " << p << std::endl;
        }
    
        return 0;
    }
    

    在这种情况下,程序输出是

    1: ,All
    2: ,you,All
    3: ,There,you,All
    

    【讨论】:

    • 如果I0 开头,则第一个main 函数存在潜在错误。你不会打印任何东西。
    • @John Smith 我没有看到任何错误。我写道,该函数的行为方式与 strchr 相同,即它找到终止零。
    • 这就是为什么我说“潜在”如果你在 for 循环中从零开始 i
    • @John Smith 无论我是从 0 还是从 1 开始,您都可以随意编写循环。这与函数本身没有任何共同之处。
    • 感谢弗拉德和约翰。这很有帮助。
    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多