[ create a new paste ] login | about

Link: http://codepad.org/GUUWK3hP    [ raw code | output | fork ]

C++, pasted on Jun 26:
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;

}


Output:
1
2
3
4
5
6
We now have sourceStringSize, which is: 10
We now have searchStringSize, which is: 4
endIndex is: 3
sourceString is enum DAY {
The str "enum DAY {" has a space after it indeed.
Press Enter to end this program.


Create a new paste based on this one


Comments: