Write a console application for the following Scenario: 1) Declare a new string
ID: 3827968 • Letter: W
Question
Write a console application for the following Scenario: 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 data Source 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. A challenge with Strings and Functions (source): In your Lab9_YourLastName folder, create a C++ Program called Lab9_Part2_YourFirstName_YourLastName.cpp Write a function "no_repetitions(...)" which removes all repetitions of characters from a string. Test the function in a suitable main program, which should be able to reproduce the following input/output: Type in a string: This string contains repeated characters The string without repetitions is: This trngcoaepd Accept input from the user with C++ getline(...) rather than our input validation method: http://www.cplusplus.com/reference/string/String/getline/Try to solve the program by using the string datatype rather than a char array char still, a string is indeed an array of chars.Explanation / Answer
Answer
Part 1 of Part 2
Below is the required code:
#include <string>
#include <sstream>
#include <vector>
using namespace std;
void split_string(const string &s, char delimiter, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delimiter)) {
elems.push_back(item);
}
}
vector<string> split_string(const string &s, char delimiter) {
vector<string> elems;
split_string(s, delimiter, elems);
return elems;
}
bool checkifCharacter(const char Character)
{
return ( (Character >= 'a' && Character <= 'z') || (Character >= 'A' && Character <= 'Z'));
//Checks if a Character is a among A-Z or a-z based on the ascii value
}
int isWordEmailAddress(const char * word)
{
if(!word) // If word is empty
return 0;
if(!checkifCharacter(word[0])) // If the first character of the word is not among A-Z, a-z
return 0;
int OffsetForAt = -1;
int OffsetForDot = -1;
unsigned int Length = strlen(word); // Length = StringLength (strlen) of word
for(unsigned int i = 0; i < Length; i++)
{
if(word[i] == '@') // If one of the characters in word is @, store it's position in OffsetForAt
OffsetForAt = (int)i;
else if(word[i] == '.') // Same as above, but with the character dot
OffsetForDot = (int)i;
}
if(OffsetForAt == -1 || OffsetForDot == -1) // If there is no Dot or a @
return 0;
if(OffsetForAt > OffsetForDot ) // If @ is after the Dot
return 0;
return !(OffsetForDot >= ((int)Length-1)); //Check if there is some other letters after the Dot
}
int main()
{
string InputString;
cout<<"Enter your string:: ";
getline(cin,UserInput)
vector<string> words;
int email_check=0; //email indicator, 1 for email id 0 for non email id words
string temp_string=InputString; //taking a temporary variable
words = split_string(temp_string,' '); //splitting the string into words on the basis of space
for( string const& word : words )
{
email_check = isWordEmailAddress(word); //checking for email id word by word
if (email_check == 1)
cout<<word<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.