954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with C++ reguarding loops and rfind

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!

LegerdemaiN
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
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 canhelp you fix it.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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?

LegerdemaiN
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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 @

LegerdemaiN
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You