#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class CenumOperations {
public:
bool containsString(const char sourceString[], const char searchString[]);
};
bool CenumOperations::containsString(const char sourceString[], const char searchString[]) {
//we are assuming 'sourceString' and 'searchString' are pointing at two null-terminated char arrays...
int sourceStringSize = 0;
while (sourceString[sourceStringSize] != '\0') {
sourceStringSize++; //increment size
}
int searchStringSize = 0;
while (searchString[searchStringSize] != '\0') { //loop while the current character isn't the null character
searchStringSize++; //increment size
}
if (searchStringSize < sourceStringSize) { //meaning the string searched for is shorter than the source string
//first check to see if the special string "A-Za-z" was passed into the 'searchString' parameter...
int searchStringIsSpecialString = strcmp(searchString, "A-Za-z");
if (searchStringIsSpecialString == 0) { //then search for a character of the source string in the range of A-Z, or a-z
for (int i = 0; i < sourceStringSize; i++) {
if (sourceString[i] >= 'A') {
if (sourceString[i] == 'A') {
return true; //at the first 'A' character
}
else if (sourceString[i] > 'A') {
if (sourceString[i] <= 'Z') {
return true; //at the first character from 'B' through 'Z'
}
else if (sourceString[i] > 'Z') {
if (sourceString[i] < 'a') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the sourceString, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters of the sourceString...
}
}
else if (sourceString[i] >= 'a') {
if (sourceString[i] == 'a') {
return true; //at the first 'a' character
}
else if (sourceString[i] > 'a') {
if (sourceString[i] <= 'z') {
return true; //at the first 'b' through 'z' character
}
else if (sourceString[i] > 'z') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the source string, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters of the sourceString...
}
}
}
}
}
}
}
}
}
//if it got this far, then search string wasn't "A-Za-z"...so we now check to see if the searchString is contained in the sourceString...
int currentIndexOfSearchString = 0;
for (int i = 0; i < sourceStringSize; i++) {
if (sourceString[i] == searchString[0]) { //then we have now found the first character of the searchString in the sourceString
for (int i2 = i; i2 < sourceStringSize && i2 < searchStringSize; i2++) {
if (sourceString[i2] != searchString[currentIndexOfSearchString]) {
if (i2 == searchStringSize - 1) { //we're at the last character of the searchString
return false;
}
else {
i = i2; //set the outer for loop iterator to the value of the inner one
break; //out of this loop at the first char of the sourceString that doesn't match the current char of the searchString
}
}
else {
//we're good so far...
}
currentIndexOfSearchString++; //increment to the next character index in the searchString
}
}
}
return true; //because if it made it to this statement, obviously the search string IS contained in the source string
}
else if (searchStringSize == sourceStringSize) { //meaning the two strings have the same length
//first check for the special "A-Za-z" string...
int searchStringIsSpecialString = strcmp(searchString, "A-Za-z");
if (searchStringIsSpecialString == 0) { //then search for a character of the source string in the range of A-Z, or a-z
for (int i = 0; i < sourceStringSize; i++) {
if (sourceString[i] >= 'A') {
if (sourceString[i] == 'A') {
return true; //at the first 'A' character
}
else if (sourceString[i] > 'A') {
if (sourceString[i] <= 'Z') {
return true; //at the first character from 'B' through 'Z'
}
else if (sourceString[i] > 'Z') {
if (sourceString[i] < 'a') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the source string, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters...
}
}
else if (sourceString[i] >= 'a') {
if (sourceString[i] == 'a') {
return true; //at the first 'a' character
}
else if (sourceString[i] > 'a') {
if (sourceString[i] <= 'z') {
return true; //at the first 'b' through 'z' character
}
else if (sourceString[i] > 'z') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the source string, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters...
}
}
}
}
}
}
}
}
}
//if it got this far, then the special string wasn't passed, and we can check to see if the source string contains the search string
int sourceStringIsSameAsSearchString = strcmp(sourceString, searchString);
if (sourceStringIsSameAsSearchString == 0) { //meaning the two strings are the same
return true;
}
else {
return false;
}
}
else if (searchStringSize > sourceStringSize) { //meaning that the string searched for is longer than the source string
//first check for the special "A-Za-z" string...
int searchStringIsSpecialString = strcmp(searchString, "A-Za-z");
if (searchStringIsSpecialString == 0) { //then search for a character of the source string in the range of A-Z, or a-z
for (int i = 0; i < sourceStringSize; i++) {
if (sourceString[i] >= 'A') {
if (sourceString[i] == 'A') {
return true; //at the first 'A' character
}
else if (sourceString[i] > 'A') {
if (sourceString[i] <= 'Z') {
return true; //at the first character from 'B' through 'Z'
}
else if (sourceString[i] > 'Z') {
if (sourceString[i] < 'a') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the source string, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters...
}
}
else if (sourceString[i] >= 'a') {
if (sourceString[i] == 'a') {
return true; //at the first 'a' character
}
else if (sourceString[i] > 'a') {
if (sourceString[i] <= 'z') {
return true; //at the first 'b' through 'z' character
}
else if (sourceString[i] > 'z') {
//check to make sure that its not the end of the source string
if (i == sourceStringSize - 1) {
return false; //since we're at the last character of the source string, and no character in the range of A-Z or a-z was found
}
else {
//continue checking characters...
}
}
}
}
}
}
}
}
}
return false; //since the string searched for has a longer string than the one being checked, meaning there is no
//possibility of the shorter string fully containing the longer string
}
return false;
}
int main() {
string sourceString("mether");
string searchString("rock");
CenumOperations enumOperationsObject;
bool containsString = enumOperationsObject.containsString(sourceString.c_str(), searchString.c_str());
if (containsString) {
cout<< '\"' << sourceString << "\" contains \"" << searchString << '\"' <<endl;
}
else {
cout<< '\"' << sourceString << "\" does NOT contain \"" << searchString << '\"' <<endl;
}
sourceString = "rock";
searchString = "enum";
containsString = enumOperationsObject.containsString(sourceString.c_str(), searchString.c_str());
if (containsString) {
cout<< '\"' << sourceString << "\" contains \"" << searchString << '\"' <<endl;
}
else {
cout<< '\"' << sourceString << "\" does NOT contain \"" << searchString << '\"' <<endl;
}
sourceString = "en";
searchString = "enum";
containsString = enumOperationsObject.containsString(sourceString.c_str(), searchString.c_str());
if (containsString) {
cout<< '\"' << sourceString << "\" contains \"" << searchString << '\"' <<endl;
}
else {
cout<< '\"' << sourceString << "\" does NOT contain \"" << searchString << '\"' <<endl;
}
cout<< "\n\nPress Enter to end this program." <<endl;
cin.get(); //make the user press Enter
return 0;
}