Created
May 22, 2011 08:22
-
-
Save Ozerich/985274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdio> | |
| #include <cstdlib> | |
| int strlen(char *text) | |
| { | |
| int i; | |
| for(i = 0; text[i] != '\0'; i++); | |
| return i; | |
| } | |
| char* str_replace(char *text, int b_pos, int in_len, char *out) | |
| { | |
| int out_len = strlen(out); | |
| if(in_len == out_len) | |
| { | |
| for(int i = 0; i < in_len; i++) | |
| text[b_pos + i] = out[i]; | |
| return text; | |
| } | |
| else if(in_len < out_len) | |
| { | |
| char *temp = new char[strlen(text) + (out_len - in_len)]; | |
| for(int i = 0; i < b_pos; i++) | |
| temp[i] = text[i]; | |
| for(int i = 0; i < out_len; i++) | |
| temp[i + b_pos] = out[i]; | |
| for(int i = b_pos + in_len; i < strlen(text); i++) | |
| temp[i + (out_len - in_len)] = text[i]; | |
| temp[strlen(text) + (out_len - in_len)] = '\0'; | |
| return temp; | |
| } | |
| else if(in_len > out_len) | |
| { | |
| char *temp = new char[strlen(text) - (in_len - out_len)]; | |
| for(int i = 0; i < b_pos; i++) | |
| temp[i] = text[i]; | |
| for(int i = 0; i < out_len; i++) | |
| temp[i + b_pos] = out[i]; | |
| for(int i = b_pos + in_len; i < strlen(text); i++) | |
| temp[i - (in_len - out_len)] = text[i]; | |
| temp[strlen(text) - 1] = '\0'; | |
| return temp; | |
| } | |
| } | |
| char up(char ch) | |
| { | |
| if(ch >= 'a' && ch <= 'z') | |
| return ch = 'A' + (ch - 'a'); | |
| return ch; | |
| } | |
| char* replace(char *text, char *in, char *out) | |
| { | |
| int cur_in = 0; | |
| int in_len = strlen(in); | |
| bool found = true; | |
| while(found) | |
| { | |
| found = false; | |
| for(int i = 0; i < strlen(text); i++) | |
| { | |
| if(up(text[i]) == up(in[cur_in])) | |
| cur_in++; | |
| else | |
| cur_in = 0; | |
| if(cur_in == in_len) | |
| { | |
| text = str_replace(text, i - in_len + 1, in_len, out); | |
| found = true; | |
| break; | |
| } | |
| } | |
| } | |
| return text; | |
| } | |
| int main() | |
| { | |
| char *in_filename = new char[1000], *out_filename = new char[1000], *in_string = new char[1000], *out_string = new char[1000]; | |
| printf("Input file: "); | |
| scanf("%s", in_filename); | |
| printf("In string: "); | |
| scanf("%s", in_string); | |
| printf("Out string: "); | |
| scanf("%s", out_string); | |
| printf("Output file:"); | |
| scanf("%s", out_filename); | |
| /* in_filename = "input.txt"; | |
| out_filename = "output.txt"; | |
| in_string = "cat"; | |
| out_string = "do";*/ | |
| FILE *f = fopen(in_filename, "r+"); | |
| char *text = new char[10000]; | |
| fgets(text, 10000, f); | |
| fclose(f); | |
| text = replace(text, in_string, out_string); | |
| f = fopen(out_filename, "w+"); | |
| fputs(text, f); | |
| fclose(f); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment