Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data conversion functions #341

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions C++/include/utils/data_validation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Data validation
---------------
This utility function contains two sub-funtions :

1) make_upper_case() : converts the characters in a string to upper case .

2) make_lower_case() : converts the characters in a string to lower case .

Time complexity
---------------
O(n), where n is the length of the string .
*/

#ifndef DATA_VALIDATION_HPP
#define DATA_VALIDATION_HPP

#include <string>

using std::string;

/*
make_upper_case()
-----------------
This function returns a given string into upper case .
Loop invariant : Selects each character and converts it to upper case .

Return value
------------
string having all the characters in upper case .
*/

string make_upper_case(string str)
{
//converting each character to upper case using for loop
for (auto &c : str)
{
c = std::toupper(c); // using the function toupper() for conversion
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than just using std::toupper() and std::tolower(), I think it would be better to implement this manually (as done here, though there are lots of ways to do it). Obviously in "real life" you'd just use the std function, but the point of this repo is (generally) to implement things ourselves (i.e., sorting a list manually rather than just calling std::sort()). More good info here.

So, please implement these utilities without using the std functions. Let me know if you have any questions!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay...I will dive right into it !

}
return str; // returning the converted string
}

/*
make_lower_case()
-----------------
This function returns a give string into lower case .
Loop invariant : Selects each character and converts it to lower case .

Return value
------------
string having all the characters in lower case .
*/

string make_lower_case(string str)
{
// converting each character to lower case using for loop
for (auto &c : str)
{
c = std::tolower(c); //using the function tolower() for conversion
}
return str; //returning the converted string
}

#endif // DATA_VALIDATION_HPP
20 changes: 20 additions & 0 deletions C++/test/utils/data_validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "third_party/catch.hpp"
#include "utils/data_validation.hpp"

TEST_CASE("Base cases", "[string][data_validation]")
{
REQUIRE(make_upper_case("cats") == "CATS");
REQUIRE(make_upper_case("dogs") == "DOGS");
REQUIRE(make_lower_case("CATS") == "cats");
REQUIRE(make_lower_case("dogs") == "DOGS");
}

TEST_CASE("Mixed cases", "[string][data-validation]")
{
REQUIRE(make_upper_case("cAts") == "CATS");
REQUIRE(make_upper_case("dOgs") == "DOGS");
REQUIRE(make_upper_case("dOGS") == "DOGS");
REQUIRE(make_lower_case("CaTS") == "cats");
REQUIRE(make_lower_case("DoGS") == "dogs");
REQUIRE(make_lower_case("dogS") == "DOGS");
}