通過(guò)C語(yǔ)言判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址
判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址
判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址。
若是則轉(zhuǎn)為16進(jìn)制,
否則輸出"error"。
例如
- 輸入:“192.41.6.20”,輸出0XC0290614
- 輸入:“abc.41.6.20” ,輸出error
- 輸入:192.41.6.20.23.234,輸出error
- 輸入:“257.41.6.20”,輸出error
注意:
Internet委員會(huì)定義了5種IP地址類型以適合不同容量的網(wǎng)絡(luò),即A類~E類。IP地址通常用“點(diǎn)分十進(jìn)制”表示成**(a.b.c.d)的形式**,其中,a,b,c,d都是0-255之間的十進(jìn)制整數(shù)。實(shí)際上是32位二進(jìn)制(01100100.00000100.00000101.00000110)
完整代碼:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxsize 100 //十進(jìn)制轉(zhuǎn)為十六進(jìn)制 int dtox(int n,char st[]){ int m; int top=-1; char c[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; while(n){ m=n%16; st[++top]=c[m]; n=n/16; } return top; } //輸出十進(jìn)制轉(zhuǎn)為十六進(jìn)制的結(jié)果 void out(char st[],int top){ while(top>-1){ printf("%c",st[top--]); } } //將十進(jìn)制的192字符串轉(zhuǎn)為int類型的192 int stringtoint(char s[]){ int sum=0; int count=1; for(int i=strlen(s)-1;i>=0;i--){ sum+=(s[i]-'0')*count; count=count*10; } return sum; } //將字符串用.分開(kāi) int split(char s[],int t[]) { char tmp[maxsize]; memset(tmp, 0, sizeof(tmp)); int count,c; int n1=0; int i=0; int n; c=0; while(i<strlen(s)){ count=0; while(s[i]!='\0'&&s[i]!='.'){ if(s[i]>='0'&&s[i]<='9'){ tmp[count++]=s[i++]; } else return -1; } n=stringtoint(tmp); if(n>255 or n<0) return -1; t[n1++]=n; memset(tmp, 0, sizeof(tmp)); i++; } return n1; } //判斷是否是正確的十進(jìn)制ip地址 void panduan(){ char s[maxsize]; scanf("%s",s); // char s[]="192.41.6.20"; // char s[]="abc.41.6.20"; // 192.41.6.20.22 int t[maxsize]; int n1; n1=split(s,t); if(n1>4||n1==-1){ printf("error"); return ; } for(int i=0;i<n1;i++){ if(i==0) printf("%#02X",t[i]); else printf("%02X",t[i]); } } int main(){ panduan(); //驗(yàn)證十六進(jìn)制轉(zhuǎn)為十進(jìn)制的函數(shù) // int n; // scanf("%d",&n); // char st[maxsize]; // int top; // top=dtox(n,st); // out(st,top); //驗(yàn)證十進(jìn)制的字符串轉(zhuǎn)為十進(jìn)制數(shù)int類型 // char s[]="192"; // printf("\n%d ", stringtoint(s)); // "192.41.6.20"轉(zhuǎn)為十六進(jìn)制0XC0290614 }
易錯(cuò)點(diǎn):16進(jìn)制格式化輸出
printf("%#02X",t[i]);
會(huì)加入前綴0X,如果2位缺的話用0填充
如6轉(zhuǎn)為0X06
20轉(zhuǎn)為0X14
printf("%x\n", 47); //輸出結(jié)果為: 2f printf("%X\n", 47); //輸出結(jié)果為: 2F printf("%#x\n", 47); //輸出結(jié)果為: 0x2f printf("%#X\n",47); //輸出結(jié)果為: 0X2F %#X推薦使用
#include <stdio.h> int main(){ int a = 120; /*按整型輸出,默認(rèn)右對(duì)齊*/ printf("%d\n",a); /*按整型輸出,補(bǔ)齊4位的寬度,補(bǔ)齊位為空格,默認(rèn)右對(duì)齊*/ printf("%4d\n",a); /*按整形輸出,補(bǔ)齊4位的寬度,補(bǔ)齊位為0,默認(rèn)右對(duì)齊*/ printf("%04d\n",a); /*按16進(jìn)制輸出,默認(rèn)右對(duì)齊*/ printf("%x\n",a); /*按16進(jìn)制輸出,補(bǔ)齊4位的寬度,補(bǔ)齊位為空格,默認(rèn)右對(duì)齊*/ printf("%4x\n",a); /*按照16進(jìn)制輸出,補(bǔ)齊4位的寬度,補(bǔ)齊位為0,默認(rèn)右對(duì)齊*/ printf("%04x\n",a); return 0; }
自己寫(xiě)函數(shù),將輸入的十進(jìn)制轉(zhuǎn)為十六進(jìn)制并且輸出
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxsize 100 //十進(jìn)制轉(zhuǎn)為十六進(jìn)制 int dtox(int n,char st[]){ int m; int top=-1; char c[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; while(n){ m=n%16; st[++top]=c[m]; n=n/16; } return top; } //輸出十進(jìn)制轉(zhuǎn)為十六進(jìn)制的結(jié)果 void out(char st[],int top){ while(top>-1){ printf("%c",st[top--]); } } int main(){ //驗(yàn)證十六進(jìn)制轉(zhuǎn)為十進(jìn)制的函數(shù) int n; scanf("%d",&n); char st[maxsize]; int top; top=dtox(n,st); out(st,top); }
函數(shù):將表示一個(gè)十進(jìn)制的字符串轉(zhuǎn)為數(shù)字類型
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxsize 100 int stringtoint(char s[]){ int sum=0; int count=1; for(int i=strlen(s)-1;i>=0;i--){ sum+=(s[i]-'0')*count; count=count*10; } return sum; } int main(){ char s[]="192"; printf("\n%d ", stringtoint(s)); }
函數(shù):將字符串用句號(hào)分裂開(kāi)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define maxsize 100 void split(char s[]) { char tmp[maxsize]; memset(tmp, 0, sizeof(tmp)); int count,c; int i=0; c=0; while(i<strlen(s)){ count=0; while(s[i]!='\0'&&s[i]!='.'){ tmp[count++]=s[i++]; // printf("%c",tmp[count-1]); } printf("%s\n",tmp); memset(tmp, 0, sizeof(tmp)); i++; } } int main(){ char s[]="192.23.32"; split(s); }
到此這篇關(guān)于通過(guò)C語(yǔ)言判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址的文章就介紹到這了,更多相關(guān)C語(yǔ)言判斷字符串是否為IP地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言的語(yǔ)法風(fēng)格與代碼書(shū)寫(xiě)規(guī)范指南
這篇文章主要介紹了C語(yǔ)言的語(yǔ)法風(fēng)格與代碼書(shū)寫(xiě)規(guī)范指南,文中主張了一些諸如變量和結(jié)構(gòu)體的命名規(guī)范等細(xì)節(jié)方面的問(wèn)題,需要的朋友可以參考下2016-02-02C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用
這篇文章主要介紹了C++中結(jié)構(gòu)體的類型定義和初始化以及變量引用,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09C++的動(dòng)態(tài)內(nèi)存管理你真的了解嗎
這篇文章主要為大家詳細(xì)介紹了C++的動(dòng)態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C語(yǔ)言實(shí)現(xiàn)控制臺(tái)版貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了c語(yǔ)言貪吃蛇控制臺(tái)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07C++ 數(shù)據(jù)結(jié)構(gòu)之水洼的數(shù)量算法
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)之水洼的數(shù)量算法的相關(guān)資料,需要的朋友可以參考下2017-06-06