c語言替換
① 在 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];
② c語言運用函數替換字元串中的數字求助大佬
#include <stdio.h>
#define N 80
/* 請在這里填寫答案 */
void f(char *c,char a)
{
for(;*c;c++)
if(*c>='0'&&*c<='9')
*c=a;
}
int main(void)
{
char c[N], a;
gets(c);
scanf("%c", &a);
f(c, a);
printf("%s", c);
return 0;
}
③ c語言 把某一字元串中的其中一段字元串替換成另一串字元串。
#include
<stdio.h>
#include
<string.h>
void
main(void)
{
char
getstr[100];
char
sendstr[100];
char
a_b[100];
char
i;
printf("please
input
the
firs
str!\n");
gets(getstr);
printf("please
input
the
second
str
!\n");
gets(sendstr);
strcpy(a_b,getstr);
//交換兩個字元串的數據
strcpy(getstr,sendstr);
strcpy(sendstr,a_b);
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str1:%c",getstr[i]);
}
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str2:%c",sendstr[i]);
}
}
這是將兩個字元串進行交換後輸出。
第二個問題則用到了數據結構了,使用一個結構體鏈表,使用strcmp()或strcmpi()比對你輸入的字元串,找到相同的輸出對應的中文字元串就好了。
其中:
strcmp()
對兩個字元串進行大小寫敏感的比較;strcmpi()
對兩個字元串進行大小寫不敏感的比較;
④ c語言中的文件替換函數
char
file[20]="
C:\a.txt
c:\b.txt";
system(file);
這是最簡單的辦法。當然你也可以寫打開源文件,再打開目標文件,讀源文件內容,寫目標文件....
⑤ C語言如何實現字元之間的替換(打出一列順序,輸出按此順序輸出)
根據你的題意。
第一行輸入是26個字母的指定順序。
第二行輸入是內容字元串。
將內容字元串中的字母按照指定順序替換成對應26個字母原順序的字母。
規則:第一行輸入必須是26個小寫母,且不能重復。
替換後的字元中字母大小寫參照原字元串。
下面是代碼:
#include<stdio.h>
#include<string.h>
#define MAXLEN 100//第二行輸入字元串的最大長度
char *inPutNewOrder();//輸入26個字母的新順序,必須為小寫字母,不能有重復。輸入成功返回字元串,失敗返回NULL
char getNewLetter(char cIn,char newOrder[27]);//獲取字母對應newOrder的原始字母,成功返回字母,失敗返回0
int main()
{
int i;
char *newOrder;//輸入新字母順序
char inputStr[MAXLEN],cIn;
while(1)
{
newOrder=NULL;
while(!newOrder)
newOrder=inPutNewOrder();
memset(inputStr,0,sizeof(char)*MAXLEN);
for(i=0;i<MAXLEN;i++)
{
cIn=getchar();
if(cIn==10)//回車結束輸入
break;
if((cIn>=65 && cIn<=90)||(cIn>=97 && cIn<=122))//如輸入的是字母,對應新字母順序,找到原字母
inputStr[i]=getNewLetter(cIn,newOrder);
else
inputStr[i]=cIn;
}
if(i==MAXLEN)
while ((cIn = getchar()) != EOF && cIn != ' ');
printf("%s ",inputStr);
}
return 0;
}
char *inPutNewOrder()//輸入26個字母的新順序,必須為小寫字母,不能有重復
{
int i=0,j,flag=1;
static char newOrder[27];
char c=0;
memset(newOrder,0,sizeof(char)*27);
while(1)
{
if(newOrder[25]!=0)
break;
c=getchar();
if(c<97 || c>122)//輸入小寫字母以外字元,標識錯誤
flag=0;
for(j=0;flag && j<i;j++)
if(newOrder[j]==c)//輸入重復,標識錯誤
flag=0;
if(!flag)//錯誤,終止輸入
{
printf("輸入格式錯誤!必須為26個小寫字母,且不能有重復,請重新輸入。 ");
while ((c = getchar()) != EOF && c != ' ');
return NULL;
}
newOrder[i]=c;
i++;
}
while ((c = getchar()) != EOF && c != ' ');
return newOrder;
}
char getNewLetter(char cIn,char newOrder[27])//獲取字母對應newOrder的原始字母,成功返回字母,失敗返回0
{
static char letters[]="abcdefghijklmnopqrstuvwxyz";
char c;
int i,flag;//flag=1大寫字母,flag=0是小寫字母
if(cIn>=65 && cIn<=90)//輸入的是大寫字母
c=cIn+32,flag=1;
if(cIn>=97 && cIn<=122)//輸入的是小寫字母
c=cIn,flag=0;
for(i=0;i<26;i++)
if(newOrder[i]==c)//在新序列中匹配大寫字母或小寫字母
{
if(flag)
return letters[i]-32;
else
return letters[i];
}
return 0;
}
⑥ c語言:如何將字元串中指定的字元替換為另一個指定字元
void
rep(char
*s,char
*s1,char
*s2)
{
char
*p;
for(;*s;s++)
/*順序訪問字元串s中的每個字元*/
{
for(p=s1;*p&&*p!=*s;p++);/*檢查當前字元是否在字元串s1中出現*/
if(*p)
*s=*(p-s1+s2);
/*當前字元在字元串s1中出現,用字元串s2中的對應字元代替s中的字元*/
}
}
不知道對於不對,你自己去試下,對了請採納,不對請往下瀏覽
⑦ C語言字元串替換
效果圖:
#include<stdio.h>
intgetLen(chara[]){
intlen=0;
while(a[len]!='