C程序将电子邮件分成主要和域名 [英] C program to split te email into main and domain name

查看:91
本文介绍了C程序将电子邮件分成主要和域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello this is my assignment i have to write a c program to split an email into main and domain name.
please anyone can help me to write a code that split the email.  





我尝试过:





What I have tried:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
   main() 
 {
	FILE*fp=fopen("m.csv","r+");
     const char s[2]=",";              // here s is constant. It can't be reasign another string but we can assign another character value at s[index]
	char*token;                        //  Char* is a pointer that points to a token. Tokens are the smallest units that make a complete C program
	int i;
	if(fp!=NULL) {
		char line[30];
		while(fgets(line,sizeof line,fp)!=NULL) {
			token=strtok(line,s);    // Strtok break the String into tokens. It search for comma and when it is found it stores the preceding value into an array and so on 
			for(i=0;i<2;i++) {                        
				if(i==0) {
					printf("%s",token);
					token=strtok(NULL,s);
				} else {
					printf("%s",(token));
				}
			}
		}
		fclose(fp);
	} 
}

推荐答案

该代码与电子邮件无关:它与(非常相关)初步)CSV处理 - 即使评论也同意。

如果你的意思是你需要将电子邮件地址xxx@yyy.zzz分成两部分:xxx和yyy.zzz然后如果您查看电子邮件地址的规则,这相对简单。这给了他们很好的清晰:电子邮件地址 - 维基百科 [ ^ ]在实践中,您可能会忽略地址的本地部分可能包含@ - 我从未见过,我也没有看到一个系统设置接受一个。如果您决定允许它们,它仍然不复杂:从字符串的右端开始,然后搜索最后一个'@'。由于域名不能包含'@'字符,因此始终标记分隔点。



所以:它并不复杂:将字符串复制到新数组中,并找到分隔本地和域部分的'@'字符。将其替换为空字符'\ 0'。

null之后的指针地址是域的开头,复制到的数组的开头是本地地址。

完成。



但这是你的作业,所以编写代码取决于你!
That code has nothing to do with emails: it is to do with (very primative) CSV handling - even the comments agree.
If you mean that you need to split an email address "xxx@yyy.zzz" into two parts: "xxx" and "yyy.zzz" then that's relatively simple, provided you look at the rules for email addresses. This gives them nice and clearly: Email address - Wikipedia[^] In practice, you can probably ignore that the local part of an address may contain '@' - I've never seen one, nor have I seen a system set up to accept one. If you do decide to permit them, it's still not complex: start from the right hand end of the string, and search back for the last '@'. Since a Domain name cannot contain the '@' characters, that will always mark the separation point.

So: it's not complex: Copy the string into a new array, and locate the '@' character which separates the local and domain parts. Replace it with a null character '\0'.
The pointer address after the null is the start of the domain, the start of the array you copied into is the local address.
Done.

But this is your homework, so writing the code is up to you!


这篇关于C程序将电子邮件分成主要和域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆