go to start Sol W4
|home |print view |recent changes |changed October 20, 2018 |
exact
|You are 54.92.160.119 <- set your identity!

Sections: Word List | countingToLower | function value table |

Here come your solutions!

When posting code you can leave out scaffolding like the generated code from unit tests or the #includes required. If there is already a solution that is different from yours, just add yours or discuss the difference. Caution: parallel edits might cause data loss (however, versioning happens and you can retrieve an overwritten edit).

Word List ^

here comes the code ...

//Solution Thomas Kistler
void wlist(std::istream& in, std::ostream& out) {
	std::string s{};
	std::vector<std::string> words{};

	while (in >> s) {
		//In case hello == Hello we could simply use:
		//std::transform(s.begin(), s.end(), s.begin(), ::tolower);
		//but we try to do it better.

		//Remove anything that is non-alpha out of the word
		s.erase(remove_if(s.begin(), s.end(),
				 [](char c) { return !std::isalpha(c); } ),
			s.end());

		words.push_back(s);
	}

	//Predicate:
	//In: 2 Strings
	//Out: true if equal in lowercase
	auto equalInLowercase = [](std::string s1, std::string s2) {
		return std::equal(s1.begin(), s1.end(),
				  s2.begin(), s2.end(),
				  [](char a, char b) {
			                return tolower(a) == tolower(b);
				  });
	};

	//Erase equal words according to predicate:
	auto last = std::unique(words.begin(), words.end(), equalInLowercase);
	words.erase(last, words.end());

	//Words only consisting out of special characters are empty. Lets remove this:
	words.erase(remove_if(words.begin(), words.end(),
			      [](std::string word) { return word.empty();}),
		    words.end());


	//Comparator / Predicate
	//In 2 strings
	//Out: lexicographical comparison of two words in lower case
	auto comparator = [](std::string s1, std::string s2) {
		return std::lexicographical_compare(s1.begin(), s1.end(),
				 	 	    s2.begin(), s2.end(),
						    [](char c1, char c2) {
							return std::tolower(c1) < std::tolower(c2);
						    });
	};

	std::sort(words.begin(), words.end(), comparator);

	std::ostream_iterator<std::string> outIt{out, ", "};
	std::copy(words.begin(), words.end(), outIt);
}


// your code here

countingToLower ^

here comes the code ...

int countingToLower(std::string & s){
	int counterUpper{0};
	for(char & c : s){
		if(isupper(c)){
			c = tolower(c);
			counterUpper++;
		}
	}
	return counterUpper;
}
// Es läuft, welcher Algorithmus wäre hier anwendbar?

// your code here

function value table ^

here comes the code ...

// your code here


|home |print view |recent changes |changed October 20, 2018 |
exact
|You are 54.92.160.119 <- set your identity!

Sol W4
go to start