Basic string manipulation question
-
Hi all, i have this string "random@mail.com", it can also be "email@yahoo.com", basically the string is an email address now how would i eliminate the characters just to get the domain name "mail.com" or "yahoo.com". Thanks, Jayjay
-
Hi all, i have this string "random@mail.com", it can also be "email@yahoo.com", basically the string is an email address now how would i eliminate the characters just to get the domain name "mail.com" or "yahoo.com". Thanks, Jayjay
Simple function:
int GetDomain (const char /*IN*/*email, char /*OUT*/*domain)
{
if (!email || !domain) return -1; // err 1
char *c= strchr ((char*)email, '@');
if (!c) return -2; // err 2
strcpy (domain, c+1);
return 0;
}...
char a[100]="";
int e= GetDomain ("sample@domain.es", a); -
Hi all, i have this string "random@mail.com", it can also be "email@yahoo.com", basically the string is an email address now how would i eliminate the characters just to get the domain name "mail.com" or "yahoo.com". Thanks, Jayjay
const char * getDomain(const char szMail)
{
const char * szDomain;
if (! szMail ) return NULL;szDomain = szMail +strlen(szMail);
while (szDomain-- != szMail )
{
if (*szDomain == '@') break;
}if (szDomain==szMail) return NULL;
return ++szDomain;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi all, i have this string "random@mail.com", it can also be "email@yahoo.com", basically the string is an email address now how would i eliminate the characters just to get the domain name "mail.com" or "yahoo.com". Thanks, Jayjay
I'm leaving error checking and other dodgy stuff to you as an exercise. :-\
const char *szEmail= "myname@mydomain.com";
std::cout<< strstr(szEmail, "@")+1 <<std::endl;Output: mydomain.com
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
I'm leaving error checking and other dodgy stuff to you as an exercise. :-\
const char *szEmail= "myname@mydomain.com";
std::cout<< strstr(szEmail, "@")+1 <<std::endl;Output: mydomain.com
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Rajesh R Subramanian wrote:
I'm leaving error checking and other dodgy stuff to you as an exercise.
That's unfair to other competitors :laugh:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Rajesh R Subramanian wrote:
I'm leaving error checking and other dodgy stuff to you as an exercise.
That's unfair to other competitors :laugh:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
That's unfair to other competitors
[Chris Tucker voice] I tell ya what: I am an unfair man. [/Chris Tucker voice]
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
I'm leaving error checking and other dodgy stuff to you as an exercise. :-\
const char *szEmail= "myname@mydomain.com";
std::cout<< strstr(szEmail, "@")+1 <<std::endl;Output: mydomain.com
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Good try, i've got another one:
char *email="myemail@mail.com";
char *domain= strchr(email,'@')?strchr(email,'@')+1: 0;:)
-
Good try, i've got another one:
char *email="myemail@mail.com";
char *domain= strchr(email,'@')?strchr(email,'@')+1: 0;:)
Welcome to the code obfuscation club. Nah, just kidding. :laugh: Nice attempt, BTW. :) [add] You know the master of obfuscations[^], right? [/add] Jus' kiddin' Carlo. ;)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
CPallini wrote:
That's unfair to other competitors
[Chris Tucker voice] I tell ya what: I am an unfair man. [/Chris Tucker voice]
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Rajesh R Subramanian wrote:
[Chris Tucker voice] I tell ya what: I am an unfair man. [/Chris Tucker voice]
[Anton Chigurh voice]: You stand to win everything. Call it [/Anton Chigurh voice] :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi all, i have this string "random@mail.com", it can also be "email@yahoo.com", basically the string is an email address now how would i eliminate the characters just to get the domain name "mail.com" or "yahoo.com". Thanks, Jayjay
Its simple using CString class
CString sEmail, sDomain; sEmail = "random@mail.com"; int nPos = sEmail.Find("@"); sDomain = sEmail.Mid(nPos + 1, sEmail.GetLength());