From a6038e88cc4f3a4c185b522c47410dfdf72c9d27 Mon Sep 17 00:00:00 2001 From: Ming Tsai Date: Tue, 9 Feb 2021 09:31:25 -0400 Subject: [PATCH] chore: delete duplicate code --- strings/c-or-cpp/strtok.cpp | 82 ------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 strings/c-or-cpp/strtok.cpp diff --git a/strings/c-or-cpp/strtok.cpp b/strings/c-or-cpp/strtok.cpp deleted file mode 100644 index a82039ac..00000000 --- a/strings/c-or-cpp/strtok.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include - -using namespace std; - -char* mystrtok(char* s, char d) -{ - // Stores the state of string - static char* input = NULL; - - // Initialize the input string - if (s != NULL) - input = s; - - // Case for final token - if (input == NULL) - return NULL; - - // Stores the extracted string - char* result = new char[strlen(input) + 1]; - int i = 0; - - // Start extracting string and - // store it in array - for (; input[i] != '\0'; i++) { - // If delimeter is not reached - // then add the current character - // to result[i] - if (input[i] != d) - result[i] = input[i]; - - // Else store the string formed - else { - result[i] = '\0'; - input = input + i + 1; // +1 for adding the delimiter - return result; - } - } - - // Case when loop ends - // add '\0' to the last token and return. - result[i] = '\0'; - input = NULL; - - // Return the resultant pointer - // to the string - return result; -} - -// Driver Code -int main() -{ - // Given string str - cout<<"Enter the string.\n"; - string inputString; - char delimiter; - getline(cin, inputString); - cout<<"Enter the delimiter token character.\n"; - cin>>delimiter; - int n=inputString.length(); - char str[n+1]; - strcpy(str, inputString.c_str()); - - // Tokenize the first string - cout<<"The tokenized string is...\n"; - char* ptr = mystrtok(str, delimiter); - - // Print current tokenized string - cout << ptr << endl; - - // While ptr is not NULL - while (ptr != NULL) { - // Tokenize the string - ptr = mystrtok(NULL, delimiter); - - if(ptr) - // Print the string - cout << ptr << endl; - } - return 0; -}