反向查找指针,以n在CSTRING中出现一个字符
Reverse Find pointer to nth occurrence of a character in cstring
如何反向查找指针以在cstring/bstr中出现nthe nth?
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
}
我已经找到了第一次也是最后一次发生搜索,但是不修改字符串反向查找nth的情况是问题。
这个
怎么样// 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;
}
怎么样?
#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
相关文章:
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何检查一个c++字符串中有多少相同的字符/数字
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 关于字符数组,我正在尝试将数组中的每个字符分配给另一个值
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用动态分配将 char* 复制到另一个字符**
- 将字符向量复制到另一个向量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- C ++读取用户输入的字符一个一个符号
- 遍历数组或从打开的文件中获取字符 - 一个比另一个有什么优势
- 将文本文件的内容一个字符一个字符地读入矢量,不跳过空白或新行
- 这是更快和更有效的,处理一个字符一个字符作为字符或流