Map the string to 0/1 according to the rules you have and search for substrings in the new string that match the mask.
How to apply mask to string in C/C++? [closed]
سؤال
I've been asigned with a homework, but I don't know what to do exactly.
Input file is randomly generated from letter from 'a' to 'z'. User will type a mask only with 0/1, for example 011011, where 1 means vowel and 0 means consonant.
Output will be all matches found in input that match the user-given mask (for example for 011011 output will be abbezz).
Any idea how to make this? I don't ask for code, but only for easiest way how to make this possible in c/c++.
Thanks
المحلول 2
نصائح أخرى
My understanding is that the requirements are to find all sequences of letters that match the mask.
Given the mask: 010 (consonant, vowel, consonant)
Here are the matches for a couple of words:
"are" - fail, first character is a vowel.
"mat" - pass, 'm' is a consonant, 'a' is a vowel, 't' is a consonant.
"mate" - fail, too many letters.
You will need to have some functions that test a letter for vowel or consonant.
Also consider using a state machine. If a test fails, you want to go back to the starting state.
Write down your algorithm first, step by step. Come up with a few test cases to verify your algorithm. After algorithm works, code it up. Use your same test cases for verifying the program.
I still do not know exactly what you are trying to accomplish, but maybe it helps to know that once you have your input in a std::string, you can iterate over the characters using iterators and any algorithm. Here is an example with std::for_each
:
#include <algorithm>
#include <iostream>
#include <string>
void applyMask(std::string::reference aCharacter)
{
// apply your mask here, character by character
if ('a' == aCharacter) {
aCharacter = 'A';
}
}
int main()
{
std::string s("stackoverflow");
std::for_each(s.begin(), s.end(), &applyMask);
std::cout << s << std::endl;
}
You might want to have a look at std::transform
, e.g. http://www.cplusplus.com/reference/algorithm/transform/