Hello I am creating a parser of sorts in C/C++ it is rather simple i just want to be able to get a string from to tags "(" and ")"
using C/C++ i know that the logic is like find first tag and increment a number every single char that is found until the next tag is found. But i suck a logic so if someone could at least give me a function that could help.
Edit:I see that C/C++ string functions are nothing alike so just C++ will do.
You seem unsure of the differences between string handling in C and in C++. Your description seems to imply wanting to do it in a C-style way.
void GetTag(const char *str, char *buffer)
{
buffer[0] = '\0';
char *y = &buffer[0];
const char *x = &str[0];
bool copy = false;
while (x != NULL)
{
if (*x == '(')
copy = true;
else if (*x == ')' && copy)
{
*y = '\0';
break;
}
else if (copy)
{
*y = *x;
y++;
}
++x;
}
}
Alternatively, the C++ way is to use the std::string which is safer because it's not fiddling with pointers, and arguably easier to read and understand.
std::string GetTag(const std::string &str)
{
std::string::size_type start = str.find('(');
if (start != str.npos)
{
std::string::size_type end = str.find(')', start + 1);
if (end != str.npos)
{
++start;
std::string::size_type count = end - start;
return str.substr(start, count);
}
}
return "";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.