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 all 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
9 changes: 9 additions & 0 deletions C++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,12 @@ target_link_libraries(disjoint_set test_runner)
add_executable(binary_search_tree
test/data_structure/tree/binary_search_tree.cpp)
target_link_libraries(binary_search_tree test_runner)

# ============================================================================
# Utilities
# ============================================================================

# Data conversion
add_executable(data_conversion
test/utils/data_conversion.cpp)
target_link_libraries(data_conversion test_runner)
82 changes: 82 additions & 0 deletions C++/include/utils/data_conversion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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>
#define UP_A 65
#define UP_Z 91

#define LOW_A 97
#define LOW_Z 122

using std::string;

/*
make_upper_case()
-----------------
This function returns a given string into upper case .
Loop invariant : Iterates through every charachter of the string and convert it to upper case

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

std::string make_upper_case(string str)
{
std::string upper; // defined a string variable to store the convereted string
for (size_t i = 0; i < str.length(); i++)
{
if (str.at(i) >= LOW_A && str.at(i) <= LOW_Z) // checking for the characters that lie between the ASCII values
{
upper += (str.at(i) - 32); // subtracting 32 to get the character in the range 65 - 97
}
else
{
upper += str.at(i);
}
}
return upper; // returning the converted string to upper case
}
/*
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 .
*/

std::string make_lower_case(string str)
{
std::string lower; // defined a variable to store the converted the string
for (size_t i = 0; i < str.length(); i++)
{
if (str.at(i) >= UP_A && str.at(i) <= UP_Z) // condition to check if the characters lie in the given ASCII range
{
lower += (str.at(i) + 32); // adding 32 to convert the characters in lower case
}
else
{
lower += str.at(i);
}
}
return lower; // returning the given string in lower case
}

#endif // DATA_VALIDATION_HPP
82 changes: 82 additions & 0 deletions C++/include/utils/data_validation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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>
#define UP_A 65
#define UP_Z 91

#define LOW_A 97
#define LOW_Z 122

using std::string;

/*
make_upper_case()
-----------------
This function returns a given string into upper case .
Loop invariant : Iterates through every charachter of the string and convert it to upper case

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

std::string make_upper_case(string str)
{
std::string upper; // defined a string variable to store the convereted string
for (size_t i = 0; i < str.length(); i++)
{
if (str.at(i) >= LOW_A && str.at(i) <= LOW_Z) // checking for the characters that lie between the ASCII values
{
upper += (str.at(i) - 32); // subtracting 32 to get the character in the range 65 - 97
}
else
{
upper += str.at(i);
}
}
return upper; // returning the converted string to upper case
}
/*
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 .
*/

std::string make_lower_case(string str)
{
std::string lower; // defined a variable to store the converted the string
for (size_t i = 0; i < str.length(); i++)
{
if (str.at(i) >= UP_A && str.at(i) <= UP_Z) // condition to check if the characters lie in the given ASCII range
{
lower += (str.at(i) + 32); // adding 32 to convert the characters in lower case
}
else
{
lower += str.at(i);
}
}
return lower; // returning the given string in lower case
}

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

TEST_CASE("Make upper case", "[string][data_conversion][make-upper-case]")
{
REQUIRE(make_upper_case("cats") == "CATS");
REQUIRE(make_upper_case("dogs") == "DOGS");
REQUIRE(make_upper_case("s3Cret") == "S3CRET");
REQUIRE(make_upper_case(" ") == " ");
REQUIRE(make_upper_case("c") == "C");
REQUIRE(make_upper_case("#er$T") == ("#ER$T"));
}

TEST_CASE("Make lower case", "[string][data_conversion][make-lower-case]")
{
REQUIRE(make_lower_case("CaTS") == "cats");
REQUIRE(make_lower_case("DoGS") == "dogs");
REQUIRE(make_lower_case("dogS") == "DOGS");
REQUIRE(make_lower_case("A@#B$%C") == "a@#b$%c");
REQUIRE(make_lower_case(" ") == " ");
REQUIRE(make_lower_case("F") == "f");
}
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");
}