Sep 30, 2016 at 6:51am UTC
1) Declare a new string named "dataSource" that has the following value:
"I wonder how they extract e-mails such as coolperson@yahoo.com or teacher@school.edu, or nonprof@dogood.org from a string of words?"
2) Create a C++ program that will extract the e-mail addresses from the dataSource string.
3) Display the extracted e-mail addresses from the user
4.Add appropriate C++ Comments to explain the logic behind your chosen method for solving the problem. Comments are also helpful for the reference of future programmers, and for you.
5. Use the appropriate data types, variables, and constant variables to solve the program
Sep 30, 2016 at 7:29am UTC
split the string in strings using the ' ' as separator.
then search for @. if you have it, its an email address
Give us some code so we can help
Last edited on Sep 30, 2016 at 7:31am UTC
Sep 30, 2016 at 8:13am UTC
Here is a crude way to do it...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool is_email(const string& s)
{
return s.find('@' ) != string::npos && s.find('.' ) != string::npos;
}
int main()
{
const string dataSource = "I wonder how they extract e-mails such as coolperson@yahoo.com "
" or teacher@school.edu, or nonprof@dogood.org from a string of words?" ;
stringstream ss {dataSource};
string extract;
while (ss >> extract)
{
if (is_email(extract))
cout << extract << '\n' ;
}
}
Last edited on Sep 30, 2016 at 2:40pm UTC
Sep 30, 2016 at 9:12am UTC
You asked for it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string datasource{ "I wonder how they extract e-mails such as coolperson@yahoo.com or teacher@school.edu, or nonprof@dogood.org from a string of words?" };
std::smatch matches{};
std::regex regex{ "[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+" };
while ( std::regex_search( datasource, matches, regex ) ) {
std::cout << matches.str( ) << "\n" ;
datasource = matches.suffix( ).str( );
}
}
coolperson@yahoo.com
teacher@school.edu
nonprof@dogood.org
Last edited on Sep 30, 2016 at 9:16am UTC
Oct 1, 2016 at 2:32am UTC
Thanks for everyone that replied really appreciate it
this what i have
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string email = "";
string dataSource = "I wonder how they extract e-mails such as me@blah.co.uk, bob@mail.bob.com, coolperson@yahoo.com or teacher@school.edu, or nonprof@dogood.org from a string of words?";
int atLoc = 0, beginspace = 0, trailspace = 0, length = 0, counter = 0;
int atOccurances = std::count(dataSource.begin(), dataSource.end(), '@');
cout << atOccurances << " email addresses found in the following data:\n\n" << dataSource << "\n\nThe email addresses were:\n\n";
do{
atLoc = dataSource.find('@',atLoc+counter);
beginspace = dataSource.rfind(" ",atLoc)+1;
trailspace = dataSource.find(" ",atLoc);
length = trailspace - beginspace;
email = dataSource.substr(beginspace, length + 1);
for(int i=0; i < length;i++) //outputs email and removes any trailing invalid character
{
if(i == length -1 && isalpha(email[i]) == false)
break;
else
cout << email[i];
}
cout << endl;
counter++;
}while(atOccurances>counter);
}