Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Basic string manipulation question

Basic string manipulation question

Scheduled Pinned Locked Moved C / C++ / MFC
comquestionlounge
11 Posts 6 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M monsieur_jj

    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

    V Offline
    V Offline
    Vijjuuu
    wrote on last edited by
    #2

    find the @ character position , copy the string from that position to end of the actual string

    1 Reply Last reply
    0
    • M monsieur_jj

      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

      J Offline
      J Offline
      Jose David Pujo
      wrote on last edited by
      #3

      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);

      1 Reply Last reply
      0
      • M monsieur_jj

        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

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        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]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • M monsieur_jj

          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

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #5

          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++]

          CPalliniC J 2 Replies Last reply
          0
          • R Rajesh R Subramanian

            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++]

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #6

            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]

            In testa che avete, signor di Ceprano?

            R 1 Reply Last reply
            0
            • CPalliniC CPallini

              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]

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #7

              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++]

              CPalliniC 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                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++]

                J Offline
                J Offline
                Jose David Pujo
                wrote on last edited by
                #8

                Good try, i've got another one:

                char *email="myemail@mail.com";
                char *domain= strchr(email,'@')?strchr(email,'@')+1: 0;

                :)

                R 1 Reply Last reply
                0
                • J Jose David Pujo

                  Good try, i've got another one:

                  char *email="myemail@mail.com";
                  char *domain= strchr(email,'@')?strchr(email,'@')+1: 0;

                  :)

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #9

                  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++]

                  1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    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++]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #10

                    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]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • M monsieur_jj

                      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

                      S Offline
                      S Offline
                      Sandeep Saini SRE
                      wrote on last edited by
                      #11

                      Its simple using CString class

                      CString sEmail, sDomain;
                      sEmail = "random@mail.com";
                      int nPos = sEmail.Find("@");
                      sDomain = sEmail.Mid(nPos + 1, sEmail.GetLength());
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups