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]!='