1. c语言中字符串的查找与替换
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20 /* 查找单词字符和输入替换单词少于20 */
#define MAXLEN 10000 /* 文章字符不大于10000 */
void main(void)
{
int i, j;
int flag; /* 用于标记匹配单词 */
int countFlag; /* 用于检查匹配计数 */
int countOne = 0; /* 记录未改文章字符个数 */
int countTwo = 0; /* 记录改后文章字符个数 */
char keyWords[SIZE]; /* 查找单词 */
char Words[SIZE];/* 替换单词 */
char strOne[MAXLEN]; /* 将未改文章的所有字符储存在里面 */
char strTwo[MAXLEN]; /* 将改后文章的所有字符储存在里面 */
FILE *fp;
printf("请输入要查找的单词: ");
gets(keyWords);
printf("请输入要替换的单词: ");
gets(Words);
if (NULL == (fp = fopen("Englishnet.txt", "rw")))/* 读文件 */
{
printf("文件打开失败!\n");
exit(1);
}
while (!feof(fp))/* 读文件 */
{
strOne[countOne++] = fgetc(fp);
}
countOne--;/* 减去最后一个文件结束字符 */
for (i=0; i<countOne; i++)
{
if (keyWords[0] == strOne[i])/* 判断查找单词第一个字符是否匹配 */
{
if ((' ' == strOne[i-1]) || ('\n' == strOne[i-1]) || (0 == i))/* 1.检查单词前的一个字符 */
{
flag = 1;
countFlag = i + 1;
for (j=1; keyWords[j]!='\0'; j++)
{
if (keyWords[j] != strOne[countFlag++])/* 是否匹配 */
{
flag = 0;
break;
}
}
if ((' ' == strOne[countFlag]) ||
('\n' == strOne[countFlag]) ||
(EOF == strOne[countFlag]))/* 2.检查单词后的一个字符 */
{
if (1 == flag)/* 若匹配,则进行拷贝 */
{
i = countFlag-1;
for (j=0; Words[j]!='\0'; j++)
{
strTwo[countTwo++] = Words[j];
}
}
}
else/* 另外 */
{
strTwo[countTwo++] = strOne[i];
}
}
else/* 另外 */
{
strTwo[countTwo++] = strOne[i];
}
}
else/* 另外 */
{
strTwo[countTwo++] = strOne[i];
}
}
fclose(fp);
if (NULL == (fp = fopen("Englishnet.txt", "w")))/* 写入文件 */
{
printf("文件打开失败!\n");
exit(1);
}
for (i=0; i<countTwo; i++)/* 写入文件 */
{
fputc(strTwo[i], fp);
}
fclose(fp);
}
请注意,在不同的运行软件中读取文件的语法有所不同,不同的就在于fopen 后面的“w” “r”有的软件支持”w+“ ”r+“,注意区分!
2. C语言字符串替换
C语言实现字符串替换函数:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
//字符串替换函数
/********************************************************************
* Function: my_strstr()
* Description: 在一个字符串中查找一个子串;
* Input: ps: 源; pd:子串
* Return : 0:源字符串中没有子串; 1:源字符串中有子串;
*********************************************************************/
char * my_strstr(char * ps,char *pd)
{
char *pt = pd;
int c = 0;
while(*ps != '\0')
{
if(*ps == *pd)
{
while(*ps == *pd && *pd!='\0')
{
ps++;
pd++;
c++;
}
}else
{
ps++;
}
if(*pd == '\0')
{
return (ps - c);
}
c = 0;
pd = pt;
}
return 0;
}
/********************************************************************
* Function: memcpy()
* Description: 复制一个内存区域到另一个区域;
* Input: src: 源;
count: 复制字节数.
* Output: dest: 复制目的地;
* Return : dest;
*********************************************************************/
void * memcpy(void * dest,const void *src,size_t count)
{
char *tmp = (char *) dest, *s = (char *) src;
while (count--)
*tmp++ = *s++;
return dest;
}
/********************************************************************
* Function: str_replace()
* Description: 在一个字符串中查找一个子串,并且把所有符合的子串用
另一个替换字符串替换。
* Input: p_source:要查找的母字符串; p_seach要查找的子字符串;
p_repstr:替换的字符串;
* Output: p_result:存放结果;
* Return : 返回替换成功的子串数量;
* Others: p_result要足够大的空间存放结果,所以输入参数都要以\0结束;
*********************************************************************/
int str_replace(char *p_result,char* p_source,char* p_seach,char *p_repstr)
{
int c = 0;
int repstr_leng = 0;
int searchstr_leng = 0;
char *p1;
char *presult = p_result;
char *psource = p_source;
char *prep = p_repstr;
char *pseach = p_seach;
int nLen = 0;
repstr_leng = strlen(prep);
searchstr_leng = strlen(pseach);
do{
p1 = my_strstr(psource,p_seach);
if (p1 == 0)
{
strcpy(presult,psource);
return c;
}
c++; //匹配子串计数加1;
printf("结果:%s\r\n",p_result);
printf("源字符:%s\r\n",p_source);
// 拷贝上一个替换点和下一个替换点中间的字符串
nLen = p1 - psource;
memcpy(presult, psource, nLen);
// 拷贝需要替换的字符串
memcpy(presult + nLen,p_repstr,repstr_leng);
psource = p1 + searchstr_leng;
presult = presult + nLen + repstr_leng;
}while(p1);
return c;
}
#define MAX 200
int main(void)
{
int i = 0;
char s[MAX] ={0}; //存放源字串
char s1[MAX]={0}; //存放子字串
char s2[MAX]={0}; //存放替换字串
char result_a[2000] = {0};//存放替换结果;
char *p,*ptm,*pr;
puts("Please input the string for s:");
scanf("%s",s);
puts("Please input the string for s1:");
scanf("%s",s1);
puts("Please input the string for s2:");
scanf("%s",s2);
ptm = s;
pr = result_a;
i = str_replace(pr,ptm,s1,s2);
printf("替换%d个子字符串;\r\n",i);
printf("替换后结果:%s\r\n",result_a);
system("pause");
}
3. 在 C语言中字符串的替换如何实现的!
1、首先输入代码:
#include <string.h>
#include <stdio.h>
/**
* 参数:
* originalString[] :原始字符串
* key[] : 待替换的字符串
* swap[] : 新字符串
*/
void replace(char originalString[], char key[], char swap[]){
int lengthOfOriginalString, lengthOfKey, lengthOfSwap, i, j , flag;
char tmp[1000];
4. C语言文本文件中字符串的查找与替换。
#include<stdio.h>
#include<string.h>
#include<math.h>
intmain()
{
charfilename[10],string1[15],string2[15],line[100];
FILE*pfile=NULL;
scanf("%s%s%s",filename,string1,string2);
pfile=fopen(filename,"r+");
if(!pfile)
{
perror("文件不存在");
return1;
}
printf("将把文件%s中字符串%s替换成%s ",filename,string1,string2);
while(!feof(pfile))
{
char*index=NULL;
fgets(line,100,pfile);
index=strstr(line,string1);
if(index)
{
intd2=strlen(string2);
intd1=strlen(string1);
printf("%s中有%s ",line,string1);
if(d1!=d2)
{
memmove(
index+d1+d2-d1,
index+d1,
strlen(line));
}
memcpy(index,string2,strlen(string2));
fseek(pfile,-strlen(line)+d2-d1,SEEK_CUR);
fputs(line,pfile);
fflush(pfile);
}
}
fclose(pfile);
return0;
}
5. C语言字符串替换
效果图:
#include<stdio.h>
intgetLen(chara[]){
intlen=0;
while(a[len]!='