C语言 检查c中字符串的最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2596072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:05:57 来源:igfitidea点击:
checking last char of string in c
提问by radar75
If I have two types of strings as:
如果我有两种类型的字符串:
const char *str1 = "This is a string with \"quotes escaped at the end\"";
const char *str2 = "This is a \"string\" without quotes at the end";
testFn(str1);
testFn(str2);
int testFn(const char *str)
{
// test & return 1 if ends on no quote
// test & return 0 if ends on quote
return;
}
I would like to test if the string ends with a quote " or not
我想测试字符串是否以引号 " 结尾
What would be a good way of testing this? Thanks
什么是测试这个的好方法?谢谢
回答by R Samuel Klatchko
Don't forget to make sure your string has at least 1 character:
不要忘记确保您的字符串至少有 1 个字符:
int testFn(const char *str)
{
return (str && *str && str[strlen(str) - 1] == '"') ? 0 : 1;
}
回答by Péter T?r?k
int testFn(const char *str)
{
return !str || !*str || str[strlen(str) - 1] != '\"';
}
回答by slacker
int testFn(const char *str)
{
if(*str && str[strlen(str + 1)] == '"')
return 0;
else
return 1;
}

