Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- std::string recordWordsFromText (std::string & text){
- std::istringstream istr(text);
- std::string onlyWords;
- istr >> onlyWords;
- for(int i = 0; i < onlyWords.size(); i++){
- if(onlyWords[i] == ',' ||
- onlyWords[i] == '.' ||
- onlyWords[i] == ';' ||
- onlyWords[i] == '!' ||
- onlyWords[i] == '?'){
- onlyWords.replace(i,1," ");
- }
- }
- }
- return onlyWords;
- }
- bool lettersMatch (std::string recordedWord, std::string singleWord, int similarityPercent){
- int len = (int)recordedWord.size();
- double matchingLetter = 0;
- for(int i = 0; i < len; i++){
- if(recordedWord[i] == singleWord[i]){
- matchingLetter ++;
- }
- }
- double matchingPercent = (matchingLetter / len) * 100;
- if(similarityPercent <= matchingPercent){
- return true;
- }
- return false;
- }
- bool areSimilar (std::string recordedWord, std::string singleWord, int similarityPercent){
- int recLen = recordedWord.size();
- int singleLen = singleWord.size();
- if( recLen == singleLen
- && recordedWord[0] == singleWord[0]
- && lettersMatch(recordedWord,singleWord,similarityPercent) == 1){
- return true;
- }
- return false;
- }
- int main(){
- std::string text;
- std::string word;
- int similarityPercent = 0;
- getline(std::cin,text);
- std::cin >> word >> similarityPercent;
- std::string newStr = recordWordsFromText(text);
- std::cout << newStr << std::endl;
- std::istringstream istr(newStr);
- std::string currentWord;
- int similarWords = 0;
- while(istr >> currentWord){
- if(areSimilar(currentWord,word,similarityPercent) == 1){
- similarWords++;
- }
- }
- std::cout << similarWords;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement