#include <string>
#include <cstring>
#include <iostream>
using namespace std;
enum ET_strippingMode {
exclusive = 0,
inclusive = 1
};
bool searchStringIsSpecialString(const char searchString[]) {
int searchStringIsSpecialString = strcmp(searchString, "A-Za-z");
if (searchStringIsSpecialString == 0) {
return true;
}
else {
return false;
}
}
bool sourceStringContainsAlphabetChar(const char sourceString[]) {
//we are assuming 'sourceString' is pointed at a null-terminated char array...
unsigned int sourceStringSize = 0;
while (sourceString[sourceStringSize] != '\0') {
sourceStringSize++; //increment size
}
char ch = 'x';
for (unsigned int i = 0; i < sourceStringSize; i++) {
ch = sourceString[i];
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
return true; //at the first char in the range of A-Z or a-z
}
else {
if (i == sourceStringSize - 1) {
return false;
}
}
}
return false;
}
bool containsString(const char sourceString[], const char searchString[]) {
//we are assuming 'sourceString' and 'searchString' are pointing at two null-terminated char arrays...
unsigned int sourceStringSize = 0;
while (sourceString[sourceStringSize] != '\0') {
sourceStringSize++; //increment size
}
unsigned 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...
if (searchStringIsSpecialString(searchString)) {
//search for a character in sourceString in the range of A-Z, or a-z:
bool trueOrFalse = sourceStringContainsAlphabetChar(sourceString);
if (trueOrFalse == true) {
return true;
}
else if (trueOrFalse == false) {
return false;
}
}
//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...
unsigned int found;
string sourceStringString = sourceString;
found = sourceStringString.find(searchString);
if (found != string::npos) { //i.e. searchString was found in searchString
return true;
}
else {
return false;
}
}
else if (searchStringSize == sourceStringSize) { //meaning the two strings have the same length
//first check to see if the special string "A-Za-z" was passed into the 'searchString' parameter...
if (searchStringIsSpecialString(searchString)) {
//search for a character in sourceString in the range of A-Z, or a-z:
bool trueOrFalse = sourceStringContainsAlphabetChar(sourceString);
if (trueOrFalse == true) {
return true;
}
else if (trueOrFalse == false) {
return false;
}
}
//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 to see if the special string "A-Za-z" was passed into the 'searchString' parameter...
if (searchStringIsSpecialString(searchString)) {
//search for a character in sourceString in the range of A-Z, or a-z:
bool trueOrFalse = sourceStringContainsAlphabetChar(sourceString);
if (trueOrFalse == true) {
return true;
}
else if (trueOrFalse == false) {
return false;
}
}
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;
}
const string stripString(const char str[], const char start, const char end, ET_strippingMode strippingMode = exclusive) {
cout<< "stripString() has been entered." <<endl;
unsigned int sizeOfStr = 0;
while (str[sizeOfStr] != '\0') {
sizeOfStr++; //increment sizeOfStr
}
cout<< "sizeOfStr is: " << sizeOfStr <<endl;
bool startHasNotBeenReachedYet = true;
bool endHasNotBeenReachedYet = true;
unsigned int i2 = 0;
string strippedString = ""; //create an empty string
for (unsigned int i = 0; i < sizeOfStr; i++) {
//loop without doing anything until 'start' character is found or until the end of the string has been reached...
if (str[i] == start) {
cout<< "str[" << i << "] == start" <<endl;
startHasNotBeenReachedYet = false; //since 'start' has now been found
if (strippingMode == exclusive) {
i2 = i+1; //begin the inner do-while loop counter at the index of the next character after the 'begin' character
}
else if (strippingMode == inclusive) {
i2 = i; //begin the inner do-while loop counter at the index of the 'begin' character
}
do {
if (i2 < sizeOfStr - 1) { //we're not at the last character of the string yet
if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the current character matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
else if (str[i2] != end) { //the current character does not match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the current character of the string to the stripped string
}
}
}
else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the current character matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the 'end' character to the stripped string
}
endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
}
else if (str[i2] != end) { //the current character does NOT match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the remaining character we want to add
}
}
}
}
else if (i2 == sizeOfStr - 1) { //we're at the last character of 'str'...
if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the last character of the string matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
else if (str[i2] != end) { //the current character does not match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the last character of the string to the stripped string
}
break; //out of the loop, since we just added the last character (if it wasn't a space)
}
}
else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
if (str[i2] == end) { //the last character of the string matches the 'end' character
cout<< "str[" << i2 << "] == end" <<endl;
strippedString += str[i2]; //add the 'end' character to the stripped string
endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
}
else if (str[i2] != end) { //the last character of the string does NOT match the 'end' character
cout<< "str[" << i2 << "] != end" <<endl;
if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
strippedString += str[i2]; //add the last character of the string to the stripped string
}
break; //out of the loop, since we just added the last character
}
}
}
i2++; //increment the inner-do while loop iterator
} while(endHasNotBeenReachedYet);
break; //out of the for loop since we already scanned through the rest of the string after 'start'
}
}
if (startHasNotBeenReachedYet) { //meaning the 'start' character was not found after iterating through the whole string
return "";
}
else {
cout<< "strippedString is: " << strippedString <<endl;
return strippedString;
}
}
int main() {
char filestreamInBuffer[] = "enum DAY {";
if (containsString(filestreamInBuffer, "enum")) {
cout<< "Yes, \"" << filestreamInBuffer << "\" does indeed contain \"enum\"." <<endl;
}
else {
cout<< "No, \"" << filestreamInBuffer << "\" does not contain \"enum\"." <<endl;
}
if (containsString(filestreamInBuffer, ";")) {
cout<< "Yes, \"" << filestreamInBuffer << "\" does indeed contain \";\"." <<endl;
}
else {
cout<< "No, \"" << filestreamInBuffer << "\" does not contain \";\"." <<endl;
}
string currentEnumName = stripString(filestreamInBuffer, 'm', '{');
cout<< "The name of the current enum is: " << currentEnumName <<endl;
return 0;
}