C 检查字符串是否与特定格式匹配
我有一个字符串定义为:C 检查字符串是否与特定格式匹配,c,c-strings,C,C Strings,我有一个字符串定义为: char *str 如何检查以验证字符串是否与格式匹配: x-y-z 其中x、y和z均为int类型 例如:字符串1-2-4应该有效,而“1-2*3”,“1-2”,“1-2-3-4”无效。实现所需功能的简单方法是使用scanf()并检查返回值。差不多 ret = scanf("%d-%d-%d", &x, &y, &z); if (ret == 3) {// match}; 简单的方法就可以了 但是,这种方法不适用于多种数据类型和较长的
char *str
如何检查以验证字符串是否与格式匹配:
x-y-z
其中x、y和z均为int类型
例如:字符串
1-2-4
应该有效,而“1-2*3”
,“1-2”
,“1-2-3-4”
无效。实现所需功能的简单方法是使用scanf()
并检查返回值。差不多
ret = scanf("%d-%d-%d", &x, &y, &z);
if (ret == 3) {// match};
简单的方法就可以了
但是,这种方法不适用于多种数据类型和较长的输入,只适用于固定格式。对于更复杂的情况,您可能需要考虑使用正则表达式库。< P>一个简单的实现您想要的方法是使用<代码> SCANFF()/CUT>并检查返回的值。差不多
ret = scanf("%d-%d-%d", &x, &y, &z);
if (ret == 3) {// match};
简单的方法就可以了
但是,这种方法不适用于多种数据类型和较长的输入,只适用于固定格式。对于更复杂的场景,您可能需要考虑使用正则表达式库。如果您需要更多信息而不只是匹配,那么可以使用循环遍历字符串。我会给你一些起始代码
int i = 0;
int correct = 1;
int numberOfDashes = 0;
while(correct && i < strlen(str)) {
if(isdigit(str[i])) {
}
else {
if(str[i] == '-') {
numberOfDashes++;
}
}
i++;
}
inti=0;
int correct=1;
int numberofdash=0;
while(正确的&i
如果您需要的信息不仅仅是匹配,那么您可以使用循环遍历字符串。我会给你一些起始代码
int i = 0;
int correct = 1;
int numberOfDashes = 0;
while(correct && i < strlen(str)) {
if(isdigit(str[i])) {
}
else {
if(str[i] == '-') {
numberOfDashes++;
}
}
i++;
}
inti=0;
int correct=1;
int numberofdash=0;
while(正确的&i
与苏拉夫的答案一致
int check( char t[] )
{
int a, b, c, d;
return sscanf( t, "%d-%d-%d-%d", &a, &b, &c, &d ) == 3;
}
int main()
{
char s[] = "1-2-4";
char t[] = "1-2-3-4";
printf( "s = %s, correct format ? %s\n", s, check( s ) ? "true" : "false" ); // <-- true
printf( "t = %s, correct format ? %s\n", s, check( t ) ? "true" : "false" ); // <-- false
}
int检查(字符t[]
{
INTA、b、c、d;
返回sscanf(t,“%d-%d-%d-%d”、&a、&b、&c、&d)==3;
}
int main()
{
字符s[]=“1-2-4”;
字符t[]=“1-2-3-4”;
printf(“s=%s,格式正确?%s\n”,s,勾选?“true”:“false”);//与苏拉夫的答案一致
int check( char t[] )
{
int a, b, c, d;
return sscanf( t, "%d-%d-%d-%d", &a, &b, &c, &d ) == 3;
}
int main()
{
char s[] = "1-2-4";
char t[] = "1-2-3-4";
printf( "s = %s, correct format ? %s\n", s, check( s ) ? "true" : "false" ); // <-- true
printf( "t = %s, correct format ? %s\n", s, check( t ) ? "true" : "false" ); // <-- false
}
int检查(字符t[]
{
INTA、b、c、d;
返回sscanf(t,“%d-%d-%d-%d”、&a、&b、&c、&d)==3;
}
int main()
{
字符s[]=“1-2-4”;
字符t[]=“1-2-3-4”;
printf(“s=%s,正确格式?%s\n”,s,检查?“true”:“false”);//您可以对特定字符串示例使用sscanf
int main()
{
int x,y,z;
char *str="1-2-4";
int a = sscanf(str, "%d-%d-%d", &x, &y, &z);
printf( "%s", (a == 3) ? "Correct format":"Incorrect format");
return 0;
}
虽然sscanf
格式不适用于这些指定字符串:
int main()
{
int x,y,z;
char *str="1-2*3"; //or "1-2" or ""1-2-3-4""
int a = sscanf(str, "%d-%d-%d", &x, &y, &z);
printf( "%s", (a == 3) ? "Correct format":"Incorrect format");
return 0;
}
为了避免这种情况,您需要像其他人已经说过的那样使用。对于特定的字符串示例,您可以使用sscanf
int main()
{
int x,y,z;
char *str="1-2-4";
int a = sscanf(str, "%d-%d-%d", &x, &y, &z);
printf( "%s", (a == 3) ? "Correct format":"Incorrect format");
return 0;
}
虽然sscanf
格式不适用于这些指定字符串:
int main()
{
int x,y,z;
char *str="1-2*3"; //or "1-2" or ""1-2-3-4""
int a = sscanf(str, "%d-%d-%d", &x, &y, &z);
printf( "%s", (a == 3) ? "Correct format":"Incorrect format");
return 0;
}
为了避免这种情况,您需要像其他人已经说过的那样使用。您自己尝试过什么?这是怎么起作用的,还是没有起作用?您的程序有什么问题?另外,请学习如何创建。仅使用普通C,或者例如正则表达式库是一个选项?我认为您应该使用正则表达式to匹配你的字符串。在谷歌上搜索一下,你会发现很多例子约翰你自己做了什么?你是怎么做的,还是没有做的?你的程序有什么问题吗?还有,请学习如何创建一个。只使用纯C,或者例如正则表达式库是一个选项?我认为你应该使用正则表达式库r表达式来匹配字符串。在谷歌上搜索一下,你会找到大量的示例Johan(可能更好地建议sscanf
?)(可能更好地建议sscanf
?)测试为“1-2-4-”
测试为“1-2-4-”