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
strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .
Strings are actually one-dimensional array of characters terminated by a null character '\0'.
Since the indexing starts from 0 so use str. charAt(str. length-1) to get the last character of string.
The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .
Don't forget to make sure your string has at least 1 character:
int testFn(const char *str)
{
return (str && *str && str[strlen(str) - 1] == '"') ? 0 : 1;
}
int testFn(const char *str)
{
return !str || !*str || str[strlen(str) - 1] != '\"';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With