新人C语言求助帖(email地址中@符号的判断)

weixin_44317435 2021-05-13 07:05:34
用c语言判断email地址下面4点,出现一种就报error 1.email地址中第一位是@符号 2.email地址中最后一位是@符号 3.email地址中没有出现@符号 4.email地址中出现2次以上@符号
...全文
178 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ctrigger 2021-05-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

// 出错返回-1,正确返回0。
int check(char *s)
{
int count = 0;
// 第一位或最后一位为@
if(s[0] == '@' || s[strlen(s)-1] == '@') return -1;

for(int i=0; i<strlen(s); i++)
{
if(s[i] == '@') count++;
}
// @没出现或2次以上
if(count == 0 || count >= 2) return -1;

return 0;
}

int main(void)
{
printf("%d\n", check("@adfaaf.com"));
printf("%d\n", check("adfaaf.com@"));
printf("%d\n", check("adfaaf.com"));
printf("%d\n", check("ad@faa@f.com"));
printf("%d\n", check("adfaa@f.com"));

return 0;
}
自信男孩 2021-05-13
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

int check_at_ch(char *src);

int main()
{
char str[] = "xx@xxxx";

if (check_at_ch(str) < 0)
printf("error!\n");

printf("%d\n", check_at_ch(str));

return 0;
}

int check_at_ch(char *src)
{
char *pstr;
unsigned int pos;

pstr = strchr(src, '@');
if (!pstr) //no '@'
return -3;

pos = pstr - src;
if (pos == 0) //at first
return -1;
else if (pos == strlen(src) - 1) //at end
return -2;
else if (strchr(pstr+1, '@')) //appear more than 2 times
return -4;
else
return 0;
}

供参考~

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧