如何对输入的密码进行校验
==================
#include <stdio.h>
int main()
{
int ip[4];
printf("请输入一个ip地址:");
while (1)
{
fflush(stdin);
if (scanf("%d.%d.%d.%d",&ip[0],&ip[1],&ip[2],&ip[3]))
{
if (0<=ip[0] && ip[0]<=255 && 0<=ip[1] && ip[1]<=255 && 0<=ip[2] && ip[2]<=255 && 0<=ip[3] && ip[3]<=255)
break;
else printf("输入的ip地址格式不对!/n请重新输入:/n");
}
else printf("输入的ip地址格式不对!/n请重新输入:/n");
}
printf("You enter the ip address is %d.%d.%d.%d!/n", ip[0], ip[1], ip[2], ip[3]);
return 0;
}
从字符串中截取密码
===============================
#include <string.h>
#include <stdio.h>
int main()
{
char str[]= "2008/05/03,192.168.1.1,test.com";
char *p = strtok(str, ",");
p = strtok(NULL, ",");
printf("%s", p);
return 0;
}
==============================
unsigned long string2ulong(const string& str){
int a, b, c, d;
sscanf(str.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d);
return a + (b << 8) + (c << 16) + (d << 24);
}
string ulong2string(ulong u){
byte * b = (byte *)&u;
char buffer[32];
sprintf(buffer, "%d.%d.%d.%d", b[0], b[1], b[2], b[3]);
return buffer;
}
==============================
#include <stdio.h>
void getip(char* from, char* to)
{
while (*from&&*from++!=',');
while ((*to++=*from)&&(*from++!=','));
*--to=0;
}
int main()
{
char from[]="2008/05/03,192.168.1.1,test.com";
char to[32]={0};
getip(from, to);
printf("%s/n", to);
return 0;
}
=============================
#include <stdio.h>
#include <conio.h>
int main()
{
int a[4]={0};
sscanf("2008/05/03,192.168.1.1,test.com","%*[^,],%d.%d.%d.%d",a,a+1,a+2,a+3);
printf("%d.%d.%d.%d ",a[0],a[1],a[2],a[3]);
getch();
return 0;
}
1, 先用 strtok 找到 IP 地址之前的逗号
2, 使用 sscanf(sResult, "%d.%d.%d.%d", &a, &b, &c, &d); 取出相应地址
谢谢大家了,学习了!