How can I get a program to find and extract an email adress from a line and replaces the @ sign with 'AT'?

I just started learning c++ and have run into a wall. I can get my screen to display everyhing from the @ sign to the end of the string, but I cant get it to stop at the.com or the space right after. I know this has somethign to do with rfind and loops but all the tutorials dont help,

I just need to see a solution or possible a different aproach, thanks!

Recommended Answers

All 4 Replies

Just use the replace method of the C++ string class.
Something like this perhaps.

#include <iostream>
#include <string>

int main()
{
    std::string email("abcdefgh@abcd.com");
    std::cout << email << std::endl;
    email.replace( email.find('@'),1, "AT" );
    std::cout << email << std::endl;

    return 0;
}

One thing to note is that only the first @ character will be replaced by this method. But I think a string with multiple @ characters is not a valid email address.

How can I get a program to find and extract an email adress from a line and replaces the @ sign with 'AT'?

1) Search for the @
2) test characters after the @ for valid domain name construct. Memorize the first position that doesn't fit.
3) test the characters before the @ for valid email name construct. Memorize the first position that doesn't fit.
4) Copy all the characters between the memorized positions.

I just started learning c++ and have run into a wall. I can get my screen to display everyhing from the @ sign to the end of the string, but I cant get it to stop at the.com or the space right after.

Why not? What are you doing wrong?

I just need to see a solution or possible a different aproach, thanks!

Actually, what you need is to show us what you are doing and maybe we can help you fix it.

int at_location = full_line.find('@');
int com_location = full_line.find('.com');
string slice = full_line.substr(at_location, com_location);

when I cout slice i get the enter string after the @.
Also how do I search from the @ to the beginnig of the word and store than in its own string?

I guess im having trouble getting find() to stop at the first space after @... annnnd having a string start at the fisrt space befor the @

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.