您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么通過(guò)C語(yǔ)言判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址”,在日常操作中,相信很多人在怎么通過(guò)C語(yǔ)言判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么通過(guò)C語(yǔ)言判斷字符串是否為點(diǎn)分十進(jìn)制的IP地址”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
判斷字符串是否為點(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 }
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); }
#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)); }
#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地址”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。