bool strIsFollowedByASpace(const char sourceString[], const char searchString[]) {
unsigned int sourceStringSize = 0;
while (sourceString[sourceStringSize] != '\0') {
sourceStringSize++; //increment sourceStringSize
}
unsigned int searchStringSize = 0;
while (searchString[searchStringSize] != '\0') {
searchStringSize++; //increment searchStringSize
}
cout<< "We now have sourceStringSize, which is: " << sourceStringSize << "\n"
<< "We now have searchStringSize, which is: " << searchStringSize << endl;
string strOfSource = sourceString; //convert c-string to a C++ string
unsigned int startIndex = strOfSource.find(searchString);
unsigned int endIndex = 0;
if (startIndex == string::npos) { //sourceString didn't fully contain searchString
return false;
}
else {
endIndex = startIndex + searchStringSize - 1; //we now have the index of the last char
cout<< "endIndex is: " << endIndex <<endl;
cout<< "sourceString is " << sourceString <<endl;
}
if (strOfSource.at(endIndex + 1) != ' ') { //the character after the last character of searchString in sourceString is not a space
cout<< "sourceString[" << endIndex << " + 1] != ' '." <<endl;
return false;
}
else {
return true;
}
}
int main() {
char sourceArray[] = "enum DAY {";
bool strHasASpaceAfter = strIsFollowedByASpace(sourceArray, "enum");
if (strHasASpaceAfter) {
cout<< "The str \"" << sourceArray << "\" has a space after it indeed." <<endl;
}
else {
cout<< "The str \"" << sourceArray << "\" does NOT have a space after." <<endl;
}
cout<< "Press Enter to end this program." <<endl;
cin.get();
return 0;
}