Back
Thay_The_Chuoi.cpp
Save
// Thay_The_Chuoi.cpp #include
#include
using namespace std; void xoaKyTu(char str[255], int k) { for (int i = k; i < strlen(str); i++) str[i] = str[i+1]; } void chenKyTu(char str[255], char x, int k) { int n = strlen(str); for (int i = n; i > k ; i--) str[i] = str[i-1]; str[k] = x; str[n+1] = '\0'; } void thayTheChuoi(char inputStr[255], char findStr[255], char replaceStr[255]) { bool find_ok = false; for (int i = 0; i < strlen(inputStr); i++) { for (int j = 0; j < strlen(findStr); j++) { if (inputStr[i+j] != findStr[j]) { find_ok = false; break; } find_ok = true; } if (find_ok) { for (int k = 0; k < strlen(findStr); k++) { xoaKyTu(inputStr, i); } for (int k = 0; k < strlen(replaceStr); k++) { chenKyTu(inputStr, replaceStr[k], i + k); } } } } int main() { char str[255]; cout<<"Nhap str: "; fflush(stdin); gets(str); char timKiem[] = "child"; char thayThe[] = "children"; thayTheChuoi(str, timKiem, thayThe); cout<<"Chuoi sau khi thay the: "<