C++ 如何读取字符串中的最后一个字符
我试图打印字符串的最后一个字符,例如str[]=“This is a example”,我试图打印带有一些函数的“example”的“e”,但没有一个像我预期的那样起作用。我知道在代码中写最后一个字符的位置号更简单,但就像在strrchr函数中一样,代码本身工作。有没有类似的功能C++ 如何读取字符串中的最后一个字符,c++,C++,我试图打印字符串的最后一个字符,例如str[]=“This is a example”,我试图打印带有一些函数的“example”的“e”,但没有一个像我预期的那样起作用。我知道在代码中写最后一个字符的位置号更简单,但就像在strrchr函数中一样,代码本身工作。有没有类似的功能 #include <stdio.h> #include <string.h> #include <iostream> using namespace std; int main ()
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main ()
{
char str[] = "This is an example";
char * pch;
pch=strrchr(str,'s');
cout<<str<<endl;
cout<<"Last time 's' was found was in position: "<<pch-str+1<<endl;
cout<<"Last character in this example is "<<str[X];
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
char str[]=“这是一个示例”;
char*pch;
pch=strrchr(str,'s');
cout来自:
终止的空字符被认为是C字符串的一部分。因此,也可以定位它以检索指向字符串结尾的指针
template <size_t N>
constexpr char LastCharacter(char (&input)[N])
{
static_assert(N >= 1, "A character array representing a string must have atleast 1 character AND a null terminator.");
return (input[N - 1] == '\0') ? input[N - 2] : input[N - 1];
}
因此,strrchr(str,'\0')[-1]
将具有最后一个字符。请注意,只有在您确定str
不是空的情况下,这才是安全的。我假设您选择char[]以避免分配或类似的内容,因此我不打算讨论std::string作为选项
三个解决方案,一个在现代C++中使用StRungIVIEW,一个使用模板;
还有一个使用std::size和index操作符
解决方案1.1:
我建议您使用它,它几乎是最佳的,并且比其他方法可读性好得多。它也不需要太多的锅炉板来处理空字符串,或者没有空终止的字符串
#include <string_view>
#include <iostream>
int main()
{
std::string_view str = "This is an example";
auto X = str.find_last_of('s');
//
// Make sure the character exists in the string
if (X != std::string_view::npos)
{
std::cout<< str << std::endl;
std::cout<< "Last time 's' was found was in position: " << X << std::endl;
}
else
{
std::cout<<"Character did not exist in string.\n";
}
if (!str.empty()) std::cout<< "Last character in this example is " << str.back();
else std::cout << "Cannot get the last character in an empty string!";
return 0;
}
这就是assert在解决方案2中处理的内容。简单:使用标准的strlen
函数,如下所示:
int main ()
{
char str[] = "This is an example";
char * pch;
pch=strrchr(str,'s');
cout<<str<<endl;
cout<<"Last time 's' was found was in position: "<<pch-str+1<<endl;
size_t X = strlen(str) - 1; // X will be the index of the last character!
cout<<"Last character in this example is "<<str[X];
return 0;
}
int main()
{
char str[]=“这是一个示例”;
char*pch;
pch=strrchr(str,'s');
cout如果您不介意使用std::string
这个代码段就可以了
#include <string>
#include <iostream>
int main() {
std::string str = "This is some text";
std::cout << str.back() << std::endl;
}
#包括
#包括
int main(){
std::string str=“这是一些文本”;
std::您确定要使用char[]
而不是std::string
?这并没有回答所问的问题。问题是如何找到字符串中的最后一个字符,而不是某个字符的最后一个实例。不,它不是固定的。检查C字符串是否以null结尾没有意义。他没有使用C字符串。他使用的是字符数组。字符数组的大小嵌入在类型中。可以使用std::size提取此信息。std::size在数组中包含空终止符,它与strlen不同。因此,如果检查最后一个索引(来自std::size),则可以确定它是否以空终止。这仅适用于char[]海报使用的类型。const char*不是这种情况。问题是该类型允许与非终止字符数组一起使用。显然,它不允许与“hello”一起使用表示法。但它与聚合初始化有关。当有标准函数给出字符串的长度时,为什么要用strrchr
来查找字符串的长度呢?请参见下面的答案!strlen很好,仍然需要在字符串末尾使用空终止符。我的解决方案1.2和2要求数组是静态的ally-defined.strlen很好,因为在该存储中长度可以更改。我认为你值得投票。你的帖子很新鲜,所以我不会编辑它,但这里有一个在线调试器链接,可以展示你整洁的X-=!!X;东西。或者只是str.back(),可能要添加空字符串的处理。很酷!这也相当于我的解决方案1.1,但我仍然喜欢它,因为std::string在这里几乎总是正确的答案。
int main ()
{
char str[] = "This is an example";
char * pch;
pch=strrchr(str,'s');
cout<<str<<endl;
cout<<"Last time 's' was found was in position: "<<pch-str+1<<endl;
size_t X = strlen(str) - 1; // X will be the index of the last character!
cout<<"Last character in this example is "<<str[X];
return 0;
}
size_t X = strlen(str); X -= !!X; // Non-zero: decrement, Zero: Leave as is
cout<<"Last character in this example is "<<str[X];
#include <string>
#include <iostream>
int main() {
std::string str = "This is some text";
std::cout << str.back() << std::endl;
}