C語言編寫函數(shù)chrn C語言編寫函數(shù)輸入輸出數(shù)組

C語言查找替換字符

 #includestdio.h

成都創(chuàng)新互聯(lián)于2013年開始,先為安次等服務建站,安次等地企業(yè),進行企業(yè)商務咨詢服務。為安次企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

#includeconio.h

#includestring.h

#includestdlib.h

void?Substitute(char?*pInput,?char?*pOutput,?char?*pSrc,?char?*pDst)

{

char????*pi,?*po,?*p;

int?????nSrcLen,?nDstLen,?nLen;

//?指向輸入字符串的游動指針.

pi?=?pInput;

//?指向輸出字符串的游動指針.

po?=?pOutput;

//?計算被替換串和替換串的長度.

nSrcLen?=?strlen(pSrc);

nDstLen?=?strlen(pDst);

//?查找pi指向字符串中第一次出現(xiàn)替換串的位置,并返回指針(找不到則返回null).

p?=?strstr(pi,?pSrc);

if(p)

{

//?找到.

while(p)

{

//計算被替換串前邊字符串的長度.

nLen?=?(int)(p?-?pi);

//?復制到輸出字符串.

memcpy(po,?pi,?nLen);

memcpy(po?+?nLen,?pDst,?nDstLen);

//?跳過被替換串.

pi?=?p?+?nSrcLen;

//?調(diào)整指向輸出串的指針位置.

po?=?po?+?nLen?+?nDstLen;

//?繼續(xù)查找.

p?=?strstr(pi,?pSrc);

}

//?復制剩余字符串.

strcpy(po,?pi);

}

else

{

//?沒有找到則原樣復制.

strcpy(po,?pi);

}

}

int?main(int?ac,?char?*av[])

{

if?(ac!=5)?{

printf("程序名?要操作的文件?新文件?查找的字符串?替換的字符串\n");

printf("示例:test.exe?1.txt?2.txt?hello?love\n");

return?0;

}

const?int?MAXSIZES?=?100;

FILE?*fpSrc,*fpDes;

char?filename1[20]="1.txt";

char?filename2[20]="2.txt";

//要求查找的字符串,替換的字符串;

char?ps[]="hello";

char?pd[]="love";

//求取所查找和替換的字符串的長度;

int?len_src=strlen(av[3]);

int?len_des=strlen(av[4]);

//定義存儲字符串緩沖區(qū);很奇怪的一點是,fgets函數(shù)不能將字符串寫入動態(tài)分配的內(nèi)存中

/*char*?Src_buf=(char*)(sizeof(char)*MAXSIZES);

char*?Cpy_buf=(char*)(sizeof(char)*MAXSIZES);

char*?Des_buf=(char*)(sizeof(char)*MAXSIZES);*/

char?Src_buf[MAXSIZES]?=?{0};

char?Cpy_buf[MAXSIZES]?=?{0};

char?Des_buf[MAXSIZES]?=?{0};

//打開文件

if((fpSrc=fopen(av[1],"r+"))==NULL)

{

printf("fail?to?open?the?file1?!\n");

exit(1);

}

if((fpDes=fopen(av[2],"a+"))==NULL)

{

printf("fail?to?open?the?file2?!\n");

exit(1);

}

//進行循環(huán)讀取

while(!feof(fpSrc))//判斷文件是否已結束;!feof(fpSrc)

{

fgets(Src_buf,MAXSIZES,fpSrc);

Substitute(Src_buf,Des_buf,av[3],av[4]);

fputs(Des_buf,fpDes);

printf("%s",Des_buf);

}

fclose(fpSrc);

fclose(fpDes);

system("pause");

return?0;

}

c語言 用函數(shù)chrn 計算c在s中出現(xiàn)的次數(shù)

#includestdio.h

chrn(char *s,char c)

{

int i=0,j=0;

while(s[i]!='\0')

{

if(s[i]=='c') j++;

i++;

}

printf("%d",j);

}

main()

{

char s[10],c;

gets(s);

c=getchar(); //應該給c 賦值

chrn(s,c); //數(shù)組是傳遞數(shù)組名。

}

大蝦們 C語言編程

#include "conio.h"

#include "stdio.h"

int chrn(char *s, char c);

int main()

{

char a[]="aaaabbaa";

printf("出現(xiàn)次數(shù):%d \n",chrn(a,'a'));

return (0);

}

int chrn(char *s, char c)

{

int i=0;

int count=0;

while (s[i])

{

if (s[i]==c)

{

count++;

}

i++;

}

return count;

}

當前名稱:C語言編寫函數(shù)chrn C語言編寫函數(shù)輸入輸出數(shù)組
瀏覽路徑:http://muchs.cn/article6/dohecig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設搜索引擎優(yōu)化、電子商務、網(wǎng)站內(nèi)鏈、網(wǎng)站導航App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設計