满意答案
dzk7823
2013.08.18
采纳率:58% 等级:12
已帮助:7420人
#include
#define FALSE 0
#define TRUE 1
int main(){
char nextChar;
int gotChar=FALSE;
int gotAt=FALSE;
int gotChar2=FALSE;
int gotDot=FALSE;
int gotChar3=FALSE;
printf("Enter your string:");
do{
scanf("%c",&nextChar);
switch(nextChar)
{
case'@':
if(gotChar)
gotAt=TRUE;
gotChar=FALSE;
break;
case'.':
if(gotChar2)
gotDot=TRUE;
gotChar2=FALSE;
break;
default://将字符的判断放到最后,根据case的执行顺序,若不是@和。则执行,唯一的缺点是不能进行非法字符的判断。
if(gotAt){
gotChar2=TRUE;
gotAt=FALSE;
}
else if(gotDot){
gotChar3=TRUE;
gotDot=FALSE;
}
else
gotChar=TRUE;
break;
}
}while(nextChar!='\n');
if(gotChar3)
printf("It's an e-mail address.\n");
else
printf("It isn't an e-mail address\n");
}
我根据楼主的意思将你的代码改成这样了,应该能实现你自己最初的想法,但是一个好的验证程序,这一点点还是不够的,二楼的思想是对的,楼主可以继续试试换一个方式来实现。祝你好运。
00分享举报