标题: 字符串替换的!(比较粗糙) [打印本页]
作者: Gin_Q 时间: 2020-3-27 20:13 标题: 字符串替换的!(比较粗糙)
本帖最后由 Gin_Q 于 2020-3-31 20:57 编辑
- a.txt文件为原始文件
- The night falls gently. And you are not here. I missing you more and more and I start getting worried as
- I stare at the door just waiting for you to surprise me with your arrival at any moment.
- Sweet delusion... you are so far away right now that all I can ask for is that time moves faster...
复制代码
- b.txt 第一列为指定字符串,第二列为替换后的字符串
- is,!@#$%^&*
- good,!@#$%^&*
- are,!@#$%^&*
- the,!@#$%^&*
- ha ha,!@#$%^&*
- some,!@#$%^&*
- get,!@#$%^&*
- has,!@#$%^&*
- more,!@#$%^&*
- bad,!@#$%^&*
复制代码
- 按b.txt中的第一列把a.txt相应位置替换为b.txt第二列
复制代码
- 结果为:
- The night falls gently. And you !@#$%^&* not here. I m!@#$%^&*sing you !@#$%^&* and more and I start !@#$%^&*ting worried as
- I st!@#$%^&* at !@#$%^&* door just waiting for you to surpr!@#$%^&*e me with your arrival at any moment.
- Sweet delusion... you !@#$%^&* so far away right now that all I can ask for !@#$%^&* that time moves faster...
复制代码
代码如下:- #if 0
- By Gin
- Dev-C++ 5.11
- .c
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <malloc.h>
-
- FILE* filepointer(const char *file,const char *mode);
- void insert_str(char* str,char* flag,size_t sl,size_t fn);
- void replace_gtr_str(char* str,char* flag,size_t sl,size_t fl);
- void replace_lss_str(char* str,char* flag,size_t subl,size_t fl);
- void replace_equ_str(char* str,char* flag,size_t fl);
- void scantext(FILE* fp, size_t* arr);
- void read_str(FILE* fp, char* sou, char* sub,size_t len);
- //字符串大小
- #define Max 1024
-
- int main(int argc,char **argv)
- {
- FILE* fpi=filepointer("a.txt","rb");
- FILE* fpo=filepointer("result.txt","wb");
- FILE* fp=filepointer("b.txt","rb");
-
- static char str[Max];
- static size_t text_s[2];//替换行数,列大小
- scantext(fp,text_s);
-
- char* soustr=(char*)calloc(text_s[0],sizeof(char)*text_s[1]);
- char* substr=(char*)calloc(text_s[1],sizeof(char)*text_s[1]);
-
- read_str(fp, soustr, substr,text_s[1]);
-
- register size_t i=0;
- register size_t len=text_s[1];
- char *p=NULL;
- while (fgets(str,Max,fpi))
- {
- for (i=0;i<text_s[0];i++)
- {
- p=str;
- p=strstr(p,soustr+i*len);
- if (p == NULL) continue;
- if (strlen(soustr+i*len) < strlen(substr+i*len)) replace_lss_str(p,substr+i*len,strlen(soustr+i*len),strlen(substr+i*len));
- else if (strlen(soustr+i*len) > strlen(substr+i*len)) replace_gtr_str(p,substr+i*len,strlen(soustr+i*len),strlen(substr+i*len));
- else if (strlen(soustr+i*len) == strlen(substr+i*len)) replace_equ_str(p,substr+i*len,strlen(substr+i*len));
- }
- fprintf(fpo,"%s",str);
- }
-
- fclose(fpi);
- fclose(fpo);
- free(soustr);
- free(substr);
- fclose(fp);
- return 0;
- }
-
- FILE* filepointer(const char *file,const char *mode)
- {
- FILE *fp;
- if (!(fp = fopen(file,mode)))
- {
- printf("open fail! [%s]:%s\n",file,strerror(errno));
- exit(EXIT_FAILURE);
- }
- return fp;
- }
- //获取文本高度,宽度以便于创建动态空间
- void scantext(FILE* fp, size_t* arr)
- {
- rewind(fp);
- char temp[Max]={"\0"};
- char *delp=",";
- char *ptr=NULL;
- register size_t y=0;
- register size_t j=0, k=0;
- while (fgets(temp,Max,fp))
- {
- y++;
- j=strlen(temp);//一行的长度
- ptr=strstr(temp,delp);
- k=strlen(ptr);//第二列长度;
- j-=k;//第一列的长度;
- arr[1] = arr[1] < (j > k ? j:k) ? (j > k ? j:k):arr[1];//记录最大值
- }
- arr[0] = y,arr[1]+=1;
- }
- void read_str(FILE* fp, char* sou, char* sub,size_t len)
- {
- rewind(fp);
- char temp[Max]={"\0"};
- char *p=NULL;
- register size_t i=0;
- register size_t j=0;
- register size_t k=0;
-
- while (fgets(temp,Max,fp))
- {
- j=strlen(temp);//总
- p=strstr(temp,",");
- k=strlen(p);//第2列
- memmove(sou+i*len,temp,j-k);
- memmove(sub+i*len,p+1,k);
- for (k-=2;;k--)
- if (*(sub+i*len+k) == 0xA || *(sub+i*len+k) == 0xD)
- *(sub+i*len+k) = '\0';//去掉换行和回车
- else
- break;
- i++;
- }
- }
- /*
- 在字符串开始插入指定字符串
- str 原字符串的起点
- flag 指定字符串
- sl 原字符串长度
- fl 指定字符串长度
- */
- void insert_str(char* str,char* flag,size_t sl,size_t fl)
- {
- memmove(str+fl,str,sl+1);//+1存放结束符'\0'
- memmove(str,flag,fl);
- }
- /*
- 被替换字符串长度大于替换字符字符串长度
- explanation:
- str=abcd,flag=abc
-
- str 原字符串的起点
- flag 替换的字符串
- sl 被替换字符长度
- fl 替换字符串长度
- */
- void replace_gtr_str(char* str,char* flag,size_t sl,size_t fl)
- {
- memmove(str+fl,str+sl,sl+1);
- memmove(str,flag,fl);
- }
- /*
- 被替换字符长度小于替换字符串长度
- 删除被替换字符串
- explanation:
- str=abc,flag=abcd
-
- str 原字符串的起点
- flag 替换的字符串
- subl 被替换字符长度
- fl 替换字符串长度
- */
- void replace_lss_str(char* str,char* flag,size_t subl,size_t fl)
- {
- register size_t temp=strlen(str)-subl;//剩余字符串长度
- memmove(str+fl,str+subl,temp+1);
- memmove(str,flag,fl);
- }
- /*
- 被替换字符串与指定字符串相等
- explanation:
- str=abc,flag=abc
-
- str 原字符串的起点
- flag 替换的字符串
- fl 替换字符长度
-
- */
- void replace_equ_str(char* str,char* flag,size_t fl)
- {
- memmove(str,flag,fl);
- }
复制代码
作者: wujunkai 时间: 2020-3-28 17:27
本帖最后由 wujunkai 于 2020-3-28 17:56 编辑
回复 1# Gin_Q - #include<cstdio>
- #define MAX 1024
-
- char Char_now=' ';
- char Str_replace_from[MAX]="no";
- char Str_replace_as [MAX]="88";
- char Str_already_mark[MAX]="";
- FILE *fin =NULL;
- FILE *fout=NULL;
- int Num_mark_distence=0;
-
- int main()
- {
- fin =fopen("text.txt" ,"r");
- fout=fopen("result.txt","w");
- // 比较字符串
- while(fscanf(fin,"%c",&Char_now)!=EOF){
- // 写入字串
- Str_already_mark[Num_mark_distence ]=Char_now;
- Str_already_mark[Num_mark_distence+1]=0;
- if( Str_replace_from[Num_mark_distence] == Str_already_mark[Num_mark_distence] ){
- // 若对比到当前位仍相同
- if( Str_replace_from[Num_mark_distence+1] == 0 ){
- // 若整个字串对比完
- fprintf(fout,"%s",Str_replace_as );
- Num_mark_distence=0;
- }
- else
- Num_mark_distence++;
- }
- else{
- // 若当前位不相同
- fprintf(fout,"%s",Str_already_mark);
- Num_mark_distence=0;
- }
- }
- fclose(fin );
- fclose(fout);
- }
复制代码
杠精路过
刚刚又想了一下,空间占用可以更小。。。。。。。为此多了一行代码- #include<cstdio>
- #define MAX 1024
-
- char Char_now=' ';
- char Char_end=' ';
- char Str_replace_from[MAX]="as";
- char Str_replace_as [MAX]="AS";
- FILE *fin =NULL;
- FILE *fout=NULL;
- int Num_mark_distence=0;
-
- int main()
- {
- fin =fopen("text.txt" ,"r");
- fout=fopen("result.txt","w");
- // 比较字符串
- while(fscanf(fin,"%c",&Char_now)!=EOF){
- // 读入字符
- if( Str_replace_from[Num_mark_distence] == Char_now ){
- // 若对比到当前位仍相同
- if( Str_replace_from[Num_mark_distence+1] == 0 ){
- // 若整个字串对比完
- fprintf(stdout,"%s",Str_replace_as );
- Num_mark_distence=0;
- }
- else
- Num_mark_distence++;
- }
- else{
- // 若当前位不相同
- Char_end=Str_replace_from[Num_mark_distence];
- Str_replace_from[Num_mark_distence]=0;
- fprintf(stdout,"%s%c",Str_replace_from,Char_now);
- Str_replace_from[Num_mark_distence]=Char_end;
- Num_mark_distence=0;
- }
- }
- fclose(fin );
- fclose(fout);
- }
复制代码
作者: Gin_Q 时间: 2020-3-31 21:01
更新了一下!比较粗糙!
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |