// Write a function that removes all occurrences of one string from another. // Function prototype given below. You may not call any C library functions. // // void RemoveString(char * pszSourceAndDest, const char * pszToRemove); // // Sample input: "Boris Yeltsin is gone.", "is" // // Sample output: "Bor Yeltsin gone." // ===== include's ===== #include <iostream> #include <stdio.h> #include "RemoveString.h" using namespace std; // ===== define's ===== #define DEBUG 1 // ===== globals ===== // ===== function prototypes ===== // ===== RemoveString() ===== // // Function to remove a string from another string. // Inputs : char *pszSourceAndDest // char *pszToRemove // Outputs : none - pszSourceAndDest will contain the stripped string // // Methodology : Use supporting functions - pszLen(), pszCpy() and strLookup() // to iteratively strLookup a string and then increment after pointer to point to // char after match is found, then pszCpy (copy) the remaining string from src to // where before pointed to. // void RemoveString(char * pszSourceAndDest, const char * pszToRemove) { // check if either char * is null - if so return if (pszLen(pszSourceAndDest) == 0) return; if (pszLen(pszToRemove) == 0) return; // check if length of pszToRemove is greater than pszSourceAndDest // if so return since we do not attempt removing strings. if (pszLen(pszToRemove) > pszLen(pszSourceAndDest)) return; // local variables int srcLen = pszLen(pszSourceAndDest); int rmvLen = pszLen(pszToRemove); int srcPos = 0; int matchAt = 0; while ((matchAt = strLookup(&pszSourceAndDest[srcPos], pszToRemove)) != -1) { srcPos += pszLen(pszToRemove); pszCpy(&pszSourceAndDest[matchAt], &pszSourceAndDest[srcPos]); } cout << "found : " << strLookup(pszSourceAndDest, pszToRemove) << endl; } // ===== pszLen() ===== // Support function to return length of a C char * (Null terminated ASCII String) const int pszLen(const char *s) { int len = 0; while (*s++) len++; return len; } // ===== pszCpy() ===== // support function to copy a C Style null terminated string from src to dest char *pszCpy(char *dest, const char *src) { if (pszLen(src) == 0) *dest = NULL; while (*src) { *dest++ = *src++;; } *dest = NULL; return dest; } // ===== strindex() ===== // support function to return an index to the first occurence of needle in haystack. int strLookup(char *haystack, const char *needle) { if (pszLen(haystack) == 0) return NULL; if (pszLen(needle) == 0) return NULL; if (pszLen(needle) > pszLen(haystack)) return NULL; bool found = false; int index = 0; int matchIndex = index; int needleIndex = 0; while ((false == found) && (haystack[index] != NULL)) { while (haystack[index] != needle[0]) { index++; } matchIndex = index; needleIndex = 0; while ((NULL != needle[needleIndex]) && (haystack[matchIndex] == needle[needleIndex])) { matchIndex++; needleIndex++; } if (NULL == needle[needleIndex]) found = true; else index++; #if DEBUG == 1 cout << "needleIndex : " << needleIndex << " matchIndex : " << matchIndex << " index : " << index << endl; #endif } if (found) return index; else return -1; }