#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char line[100]; /* buffer for input data */
char four[][4] = {"damn","fuck","mofo","cock","arse"};
char polite[][4] = {"mali","love","like","well","kind"};
char temp[2000]; /* char array for store data */
char out[2000]; /* char array after change word */
FILE *in_file; /* file to read */
FILE *out_file; /* file to write */
void checkFour();
void readfile();
void clearT();
void concatT();
void checkAndChange();
int strcompare();
int main(int argc, char *argv[])
{
printf("Enter file to read name\n");
fgets(line, sizeof(line), stdin);
line[strlen(line)-1] = '\0';
in_file = fopen(line,"r");
printf("Enter file to save name\n");
fgets(line, sizeof(line), stdin);
line[strlen(line)-1] = '\0';
out_file = fopen(line,"w");
readfile(temp,in_file);
printf("%s\n",temp);
checkFour(temp,out);
printf("%s\n",out);
fputs(out,out_file);
fclose(in_file);
fclose(out_file);
return 0;
}
void checkFour(char *tem,char *out){
char ch; /* temporary char */
int length = strlen(tem);
int i; /* control for loop */
char word[20]; /* temporary word for check four */
clearT(word);
char *word_ptr = &word[0];
int cfour = 0; /* count word for four letter */
char other[2]; /* char array for delimeter */
char *out_ptr = &out[0];
for(i = 0; i < length; ++i){
ch = *tem;
if(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='.'
&&ch!='('&&ch!=')'&&ch!=','&&ch!='"'
&&ch!=':'){
*word_ptr = ch;
++cfour;
++word_ptr;
}else{
if(cfour > 4 || cfour < 4){
concatT(&out_ptr,word);
}else{ /* check it with four array */
checkAndChange(word);
concatT(&out_ptr,word);
}
cfour = 0;
other[0] = ch;
other[1] = '\0';
concatT(&out_ptr,other);
clearT(word);
word_ptr = &word[0];
}
++tem;
}
}
void readfile(char *str,FILE *in_file){
char ch; /* temporary char */
while(1){
ch = fgetc(in_file);
if(ch == EOF)
break;
*str = ch;
++str;
}
}
void clearT(char *word){
int i; /* control for loop */
for( i = 0; i < 20; ++i){
*word = '\0';
++word;
}
}
void concatT(char **out,char *word){
int length = strlen(word);
int i; /* control for loop */
for( i = 0; i < length; ++i){
*(*out) = *word;
++(*out); ++word;
}
}
void checkAndChange(char *word){
int i,j; /* control for outer and inner loop */
char ch[4]; /* temporary char */
char ch2[4]; /* temporary copy of word */
strcpy(ch2,word); /* copy it avoid test for another position */
printf("%s ",word);
for( i = 0; i < 5; ++i){
for( j = 0; j < 4; ++j){
ch[j] = four[i][j];
}
if(strcompare(ch2,ch)==0){/* change it with polite word */
for( j = 0; j < 4; ++j){
*word = polite[i][j];
++word;
}
break;
}
}
}
int strcompare(char *ch,char *ch2){
int i;
int total = 0; /* total of different value */
int diff = 7; /* if has different character make sure to show its diff */
for( i= 0; i < 4; ++i){
total += (*ch - *ch2) * diff;
++ch; ++ch2;
diff *= diff;
}
if(total==0){
return 0;
}else{
return -1;
}
}
จาก code อันนี้ จะเห็นว่า ผมต้อง override strcmp ใหม่
เพราะ ใช้ ของที่มีมา มันไม่ตรวจว่า เท่ากัน ผมแปลกใจ มาก ๆ เลย
ไม่รู้ เพราะ ตัวอักษร ใน code กับ ใน file มันต่างกัน อย่างไร
แต่ เปลี่ยนแล้ว มันก็ดีขึ้น แต่ยังไม่แน่ใจ ทั้งหมด
เพราะมันทำได้แค่ เกือบ ๆ เท่านั้น ผมคิดว่า นะ










