c++ 實現部分string.h函式
阿新 • • 發佈:2019-01-08
#include <iostream> using namespace std; void Strcpy(char* ch1,const char* chSource) { int i = 0; while (chSource[i] != '\0') { ch1[i] = chSource[i]; i++; } ch1[i] = chSource[i]; } int Strcmp(const char* ch1, const char* ch2) { int i = 0; int j = 0; int r = 0; //The default value is equals. do { if (ch1[i] > ch2[j]) { r = 1; break; } if (ch1[i] < ch2[j]) { r = -1; break; } i++; j++; } while (ch1[i] != '0' && ch2[j] != '0'); return r; } void Strcat(char* destination, const char* source) { int i = 0; while (destination[i] != '\0') { i++; } int j = 0; while (source[j] != '\0') { destination[i] = source[j]; i++; j++; } destination[i] = '\0'; } int Strfnd(const char* child, const char* parent) { int r = -1; for (int i = 0; parent[i] != '\0'; i++) { bool flag = true; for (int j = 0; child[j] != '\0'; j++) { if (child[j] != parent[i + j]) { flag = false; break; } } if (flag) { r = i; break; }; } return r; } int main() { cout << Strfnd("123", "35134613451234134") << endl; cout << Strfnd("123", "351346134512234134") << endl; system("pause"); }