IP地址的倒序输出

    技术2022-05-12  1

    #include <stdio.h> #include <stdlib.h> #include <string.h> static char * IpReverse(char *psIp) { int niIpLen, niLen; char delim[] = "."; char * p = NULL; char *psIpTmp = NULL; niIpLen = strlen(psIp); psIpTmp = (char *)malloc(niIpLen + 1); memset(psIpTmp, 0, niIpLen + 1); int i = 0, j = 0; /*把IP地址倒置,192.168.26.32,变成23.62.861.291赋值给psIpTmp*/ for(i = niIpLen - 1; i >= 0; i--, j++) psIpTmp[j] = psIp[i]; memset(psIp, 0, niIpLen);//清空原IP字符串 /*23.62.861.291 -> 32.26.168.192*/ j = 0; p = strtok(psIpTmp, delim); do { niLen = strlen(p); for(i = niLen - 1; i >= 0; i--, j++) psIp[j] = p[i]; if(j < niIpLen) psIp[j++] = '.';//防止最后一个字符为"." }while(NULL != (p = strtok(NULL, delim))); free(psIpTmp); psIpTmp = NULL; return psIp; } int main() { char IP[] = "192.168.26.32"; printf("IP = %s/n", IpReverse(IP)); return 1; }


    最新回复(0)