美文网首页
C语言判断给定的字符串是否为合法的ip地址的代码

C语言判断给定的字符串是否为合法的ip地址的代码

作者: giveyoume | 来源:发表于2021-11-09 13:08 被阅读0次

下面内容是关于C语言判断给定的字符串是否为合法的ip地址的内容。

#include <stdio.h> 

#include <string.h> 

int main(void) 

    char str[31],temp[31]; 

    int a,b,c,d; 

    while(gets(str)!=NULL) 

    { 

        if(sscanf(str, "%d.%d.%d.%d ",&a,&b,&c,&d)==4 &&  a>=0  &&  a<=255 &&  b>=0  &&  b<=255 &&  c>=0  &&  c<=255 &&  d>=0  &&  d<=255) 

        { 

            if(strcmp(temp,str)==0) 

            { 

                printf("YESn"); 

            } 

            else 

            { 

                printf("NOn"); 

            } 

        } 

        else 

        { 

            printf("NOn"); 

        } 

    } 

    return 0; 

相关文章

网友评论

      本文标题:C语言判断给定的字符串是否为合法的ip地址的代码

      本文链接:https://www.haomeiwen.com/subject/dvvmzltx.html