一个txt存英文文章。一个txt存放着几个专有名词。如何用c语言 编写程序将英文文章中的所有专有名词都变做第二个txt中的形式!!5555555555、跪求大神帮助!!不胜感激!
如文章中的一段:
THE BEDouins PROUDLY claim that they are the original Arabs having
descended from two main tribes thousands of yeas ago. one of the tribes
settled in mountainous South-eastern Arabia and the other tribe settled in
north-central Arabia.
专有名词:
BedouINS
THE
NOMADS
#include "stdio.h"
#include "string.h"
#include "malloc.h"
/* 字符串转换成小写 */
int lower(char * str)
{
char *p = str;
while(*p)
{
if('A' <= *p && *p <= 'Z') *p += 0x20;
p++;
}
return p - str;
}
void cpypat(char * out, const char * in)
{
*out++ = ' ';
while(('a' <= *in && *in <= 'z') || ('A' <= *in && *in <= 'Z')) *out++ = *in++;
*out++ = ' ';
*out = 0;
}
/* 替换字符串 */
int replace(char * str, char ** pat, int patN)
{
int i, pos;
char *strbuf;
char **patbuf;
char * find;
/* 申请内存 */
strbuf = (char * )malloc((strlen(str) + 1) * sizeof(char));
if(strbuf == NULL) return 0;
patbuf = (char **)malloc(patN * sizeof(char *));
if(patbuf == NULL) {free(strbuf); return 0;}
for(i=0; i<patN; i++)
{
patbuf[i] = (char * )malloc((strlen(pat[i]) + 1) * sizeof(char));
if(patbuf[i] == NULL) break;
}
if(i != patN)
{
for(i--; i>=0; i--) free(patbuf[i]);
free(patbuf);
free(strbuf);
}
/* 复制字符串,并将其转换为小写 */
strcpy(strbuf, str);
lower(strbuf);
for(i=0; i<patN; i++)
{
strcpy(patbuf[i], pat[i]);
lower(patbuf[i]);
}
/* 开始替换 */
for(i=0; i<patN; i++)
{
find = strstr(strbuf, patbuf[i]); /* 查找字符串 */
while(find)
{
pos = find - strbuf;
memcpy(str + pos, pat[i], strlen(pat[i])); /* 替换字符 */
find = strstr(find + 1, patbuf[i]); /* 查找下一个 */
}
}
/* 释放内存 */
for(i=0; i<patN; i++) free(patbuf[i]);
free(patbuf);
free(strbuf);
return 1;
}
/* 从指定的文件中读取模式串 */
char ** readPat(const char * fileName)
{
char buf[1024], count = 0, i;
FILE *fp = fopen(fileName, "r");
char ** r;
if(fp==NULL) {printf("<%s> open error!\n"); return NULL;}
/* 统计需要替换的字符串数量 */
while(!feof(fp))
{
fgets(buf, 1024, fp);
count++;
}
// 申请内存
r = (char **)malloc((count + 1) * sizeof(char *));
r[count] = NULL;
if(r == NULL)
{
fclose(fp);
return NULL;
}
rewind(fp); /* 初始化文件 */
i = 0;
while(!feof(fp))
{
fgets(buf, 1024, fp);
r[i] = (char *)malloc((strlen(buf) + 4) * sizeof(char));
cpypat(r[i++], buf);
}
fclose(fp);
if(i == count) return r; /* 成功 */
/* 失败,释放内存 */
for(i--; i>=0;i--) free(r[i]);
free(r);
return NULL;
}
int process(const char * infile, const char * patfile, const char * outfile)
{
int patN;
char buf[4096];
char ** pat;
FILE *fpi = fopen(infile, "r");
FILE *fpo = fopen(outfile, "w");
if(fpi == NULL || fpo == NULL)
{
printf("<%s> or <%s> open error!\n");
fcloseall();
return 0;
}
pat = readPat(patfile);
patN = 0;
while(pat[patN]) patN++;
while(!feof(fpi))
{
fgets(buf, 4096, fpi);
replace(buf, pat, patN);
fputs(buf, fpo);
}
printf("success.\n");
fcloseall();
for(patN--; patN>=0; patN--) free(pat[patN]);
free(pat);
return 1;
}
int main(int argc, char* argv[])
{
char in[200], out[200], pat[200];
printf("please input text file Name: ");
scanf("%s", in);
printf("please input pattern file Name: ");
scanf("%s", pat);
printf("please input out file Name: ");
scanf("%s", out);
return process(in, pat, out);
}
#include "string.h"
#include "malloc.h"
/* 字符串转换成小写 */
int lower(char * str)
{
char *p = str;
while(*p)
{
if('A' <= *p && *p <= 'Z') *p += 0x20;
p++;
}
return p - str;
}
void cpypat(char * out, const char * in)
{
*out++ = ' ';
while(('a' <= *in && *in <= 'z') || ('A' <= *in && *in <= 'Z')) *out++ = *in++;
*out++ = ' ';
*out = 0;
}
/* 替换字符串 */
int replace(char * str, char ** pat, int patN)
{
int i, pos;
char *strbuf;
char **patbuf;
char * find;
/* 申请内存 */
strbuf = (char * )malloc((strlen(str) + 1) * sizeof(char));
if(strbuf == NULL) return 0;
patbuf = (char **)malloc(patN * sizeof(char *));
if(patbuf == NULL) {free(strbuf); return 0;}
for(i=0; i<patN; i++)
{
patbuf[i] = (char * )malloc((strlen(pat[i]) + 1) * sizeof(char));
if(patbuf[i] == NULL) break;
}
if(i != patN)
{
for(i--; i>=0; i--) free(patbuf[i]);
free(patbuf);
free(strbuf);
}
/* 复制字符串,并将其转换为小写 */
strcpy(strbuf, str);
lower(strbuf);
for(i=0; i<patN; i++)
{
strcpy(patbuf[i], pat[i]);
lower(patbuf[i]);
}
/* 开始替换 */
for(i=0; i<patN; i++)
{
find = strstr(strbuf, patbuf[i]); /* 查找字符串 */
while(find)
{
pos = find - strbuf;
memcpy(str + pos, pat[i], strlen(pat[i])); /* 替换字符 */
find = strstr(find + 1, patbuf[i]); /* 查找下一个 */
}
}
/* 释放内存 */
for(i=0; i<patN; i++) free(patbuf[i]);
free(patbuf);
free(strbuf);
return 1;
}
/* 从指定的文件中读取模式串 */
char ** readPat(const char * fileName)
{
char buf[1024], count = 0, i;
FILE *fp = fopen(fileName, "r");
char ** r;
if(fp==NULL) {printf("<%s> open error!\n"); return NULL;}
/* 统计需要替换的字符串数量 */
while(!feof(fp))
{
fgets(buf, 1024, fp);
count++;
}
// 申请内存
r = (char **)malloc((count + 1) * sizeof(char *));
r[count] = NULL;
if(r == NULL)
{
fclose(fp);
return NULL;
}
rewind(fp); /* 初始化文件 */
i = 0;
while(!feof(fp))
{
fgets(buf, 1024, fp);
r[i] = (char *)malloc((strlen(buf) + 4) * sizeof(char));
cpypat(r[i++], buf);
}
fclose(fp);
if(i == count) return r; /* 成功 */
/* 失败,释放内存 */
for(i--; i>=0;i--) free(r[i]);
free(r);
return NULL;
}
int process(const char * infile, const char * patfile, const char * outfile)
{
int patN;
char buf[4096];
char ** pat;
FILE *fpi = fopen(infile, "r");
FILE *fpo = fopen(outfile, "w");
if(fpi == NULL || fpo == NULL)
{
printf("<%s> or <%s> open error!\n");
fcloseall();
return 0;
}
pat = readPat(patfile);
patN = 0;
while(pat[patN]) patN++;
while(!feof(fpi))
{
fgets(buf, 4096, fpi);
replace(buf, pat, patN);
fputs(buf, fpo);
}
printf("success.\n");
fcloseall();
for(patN--; patN>=0; patN--) free(pat[patN]);
free(pat);
return 1;
}
int main(int argc, char* argv[])
{
char in[200], out[200], pat[200];
printf("please input text file Name: ");
scanf("%s", in);
printf("please input pattern file Name: ");
scanf("%s", pat);
printf("please input out file Name: ");
scanf("%s", out);
return process(in, pat, out);
}
温馨提示:答案为网友推荐,仅供参考