[ create a new paste ] login | about

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

C++, pasted on Jun 2:
#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...
        size_t found;
        string sourceStringString = sourceString;
        found = sourceStringString.find(searchString);
        if (found != string::npos) { //i.e. searchString wasn't found in searchString
            return true;
        }

        else {
            return false;
        }
    }

    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;

}


Output:
1
2
3
4
5
6
"mether" does NOT contain "rock"
"rock" does NOT contain "enum"
"en" does NOT contain "enum"


Press Enter to end this program.


Create a new paste based on this one


Comments: