Last active
May 9, 2017 11:52
-
-
Save henrybear327/138bcb8b1637cbabb9f70737c37b5c29 to your computer and use it in GitHub Desktop.
find and replace.c
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
/*405415039 ������*/ | |
#include <memory.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main() | |
{ | |
FILE *HW = fopen("input.txt", "r"); | |
FILE *hw = fopen("output.txt", "w"); | |
int count = 0, i, j, k = 0, c = 0, f = 0; | |
char search[15], replace[15], word[15], big[15] = {0}, big2[15] = {0}; | |
printf("\nWelcome to the search and Replace program!\nKey word to search and " | |
"replace:\n=>"); | |
scanf("%s %s", search, replace); | |
printf("\nProcessing...Done!!!\n\n========Result========\n"); | |
while (fscanf(HW, "%s", word) != EOF) { | |
memset(big, 0, strlen(big)); | |
memset(big2, 0, strlen(big2)); | |
int bigLen = strlen(word); | |
int big2Len = strlen(search); | |
// toupper case | |
for (i = 0; i < bigLen; i++) | |
if ('a' <= word[i] && word[i] <= 'z') | |
big[i] = word[i] - 'a' + 'A'; | |
else | |
big[i] = word[i]; | |
for (i = 0; i < big2Len; i++) | |
if ('a' <= search[i] && search[i] <= 'z') | |
big2[i] = search[i] - 'a' + 'A'; | |
else | |
big2[i] = search[i]; | |
printf("%s\n", big); | |
printf("%s\n", big2); | |
// for every position, compare if "big2" is contained in "big + i" | |
for (i = 0; i < bigLen; i++) { | |
printf("%s is in %s => %s\n", big2, big + i, | |
strncmp(big2, big + i, big2Len) == 0 ? "YES" : "NO"); | |
if (strncmp(big2, big + i, big2Len) == 0) { | |
count++; | |
break; | |
} | |
} | |
k = 0; | |
if ((strcmp(search, word)) == 0) { | |
c++; | |
strcpy(word, replace); | |
} | |
fprintf(hw, "%s ", word); | |
printf("Matching words found: %d words\nWords replaced %d words\n\n\n", | |
count, c); | |
} | |
printf("Matching words found: %d words\nWords replaced %d words\n", count, c); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment