⑴ c語言編程-字元替換
問題出在這一句:
p = (unsigned char *)strchr((char *)xx[i], '\n ') ;
注意'\n ',單括弧內多了個空格!
還有,樓主按行讀取文件並且按行處理,我覺得緩沖區方面有潛在bug,最好換種方式處理。
⑵ c語言:如何將字元串中指定的字元替換為另一個指定字元
需要准備的材料分別有:電腦、C語言編譯器。
1、首先,打開C語言編譯器,新建一個初始.cpp文件,例如:test.cpp。
⑶ 在 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語言中,宏替換的替換規則
簡單來說:宏定義又稱為宏代換、宏替換,簡稱「宏」。宏替換是C/C++的預處理中的一部分,在C++標准中有4條規則來定義替換。
規則1:實參替換。
本條規則描述帶參數的宏的替換過程。
對於宏定義中的形參,在替換列表中,如果不是作為#或##的操作數,那麼將對應實參完全
展開(相當於對實參進行求值),然後將替換列表中的形參替換掉.如果是#或##的操作數,
那麼不進行替換。
規則2:多次掃描。
在所有的形參替換為實參後,對結果進行再次掃描,如果發現還有可替換的宏,則進行替換,
否則中止。
規則3:遞歸替換抑制。
如果在替換列表中發現當前正在展開的宏的名字,那麼這里不進行替換.更進一步,在嵌套
的替換過程中發現已經替換過的宏的名字,則不進行替換。
規則4:遞歸預處理抑制。
如果替換後的結果形成預處理指令,則不執行這條預處理指令。
看幾個C++標准中的例子:
#define x 3
#define f(a) f(x * (a))
#undef x
#define x 2
#define g f
#define z z[0]
#define h g(~
#define m(a) a(w)
#define w 0,1
#define t(a) a
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
g(x+(3,4)-w) | h 5) & m(f)^m(m);
其結果分別是
f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
f(2 * (2+(3,4)-0,1)) | f(2 * ( ~ 5)) & f(2 * (0,1))^m(0,1);
對於第一個,主要在於t(t(g)(0) + t)(1)的展開。
容易計算出最外層的t的實參是f(2 * (0)) + t,而作為t的參數傳入時其中的t是
正在被展開的宏,所以根據規則3,不對這個t進行處理,保持不變,得到f(2 * (0)) + t(1)。
對於第二個,h 5)被替換為g(~5),應用規則2,被替換為f(2 * ( ~ 5))。
而m(m)首先被替換為m(w),然後應用規則2再次進行替換,但是m已經是替換過的了,所以保持
不變,只對w進行替換。
#define str(s) # s
#define xstr(s) str(s)
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s",
x ## s, x ## t)
#define INCFILE(n) vers ## n /* from previous #include example */
#define glue(a, b) a ## b
#define xglue(a, b) glue(a, b)
#define HIGHLOW "hello"
#define LOW LOW ", world"
debug(1, 2);
fputs(str(strncmp("abc d", "abc", 』4』) /* this goes away */
== 0) str(: @ ), s);
#include xstr(INCFILE(2).h)
glue(HIGH, LOW);
xglue(HIGH, LOW)
其結果分別是
printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
fputs("strncmp("abc\0d", "abc", 』\4』) = = 0" ": @ ", s);
#include "vers2.h"
"hello";
"hello" ", world"
關鍵是glue和xglue.
對於glue(HIGH, LOW),首先有一個規則1的抑制,得到HIGHLOW;的結果,然後二次掃描,得到
"hello";
對於xglue(HIGH, LOW)沒有抑制效果,所以對參數求值,分別得到HIGH和LOW ", world",即
glue(HIGH, LOW ", world")。
然後進行連接操作得到HIGHLOW ", world",最後再掃描一次得到"hello" ", world"
如果考慮字元串的自然的連接,就可以得到"hello, world"了。
(4)c語言編程中添加變成替換擴展閱讀
宏語言是一類編程語言,其全部或多數計算是由擴展宏完成的。宏語言並未在通用編程中廣泛使用,但在文本處理程序中應用普遍。例如, C preprocessor C預處理器Internet Macros(iOpus) M4(如前所述,源於AT&T,捆綁於Unix)
宏定義
c程序提供的預處理功能之一。包括帶參數的宏定義和不帶參數的宏定義。具體是指用一個指定的標志符來進行簡單的字元串替換或者進行闡述替換。形式為:
#define標志符[(參數表)] 字元串
宏名
在上定義中的標志符被稱為「宏名」。
宏展開
在c程序編譯時將宏名替換成字元串的過程稱為「宏展開」。
宏語言是一類編程語言,其全部或多數計算是由擴展宏完成的。宏語言並未在通用編程中廣泛使用, 但在文本處理程序中應用普遍。例如,
C preprocessorC 預處理器
Internet Macros(iOpus)
M4(如前所述,源於AT&T,捆綁於Unix)
⑸ c語言里插個英文會把後面的給替換了 怎麼辦
編輯器變成替換模式了,按一下insert鍵,恢復成插入模式
⑹ 在c語言編程中如何實現程序對文本文件中字元串進行替換及生成新的文本文件
我以前剛學C++的時候寫過一個相似的程序,如果你要的是純C語言下的編程,那麼你就參考一下,這個演算法的原理是一樣的,即讀入一個字元就顯示出來。當然你也可以考慮其他實現方式。這個C++的程序中,和C語言區別的主要是有些輸入和輸出不太一樣。還有system("pause")這個是調用系統暫停功能,可能在TC等編譯環境下不能使用,可以考慮使用getch()替換。至於system("cls")是清屏。
相關功能函數為Display_text(),
#include<iostream>
#include<fstream>
#include <iomanip>
#include<windows.h>
using namespace std;
#define MaxSize 65535
int tag[100]; //匹配關鍵字的字元下標,設定最多找到100個關鍵字
typedef struct
{
char data[MaxSize]; //記錄字元值
int len; //保存有效字元串長度
}SqString;
void MainMenu(); //顯示主菜單
void Select_function(char op); //功能選擇
void Display_text(); //顯示本文內容
void Count_ch(); //統計字元數,空格數,行數
void Search_KeyWord(); //檢索關鍵字
void Replace_KeyWord(); //替換關鍵字
void index(SqString s,SqString t); //簡單匹配演算法(BF)
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor); //顏色函數
int main()
{
MainMenu();
return 0;
}
void MainMenu() //顯示主菜單
{
char op;
cout<<"I——打開文本文件\n";
cout<<"T——統計\n";
cout<<"S——檢索\n";
cout<<"R——替換\n";
cout<<"Q——退出\n\n";
cout<<"請選擇:";
cin>>op;
Select_function(op);
}
void Select_function(char op) //功能選擇
{
switch(op)
{
case 'i':
case 'I':Display_text();break;
case 't':
case 'T':Count_ch();break;
case 's':
case 'S':Search_KeyWord();break;
case 'r':
case 'R':Replace_KeyWord();break;
case 'q':
case 'Q':exit(0);
default:cout<<"輸入錯誤,請重新選擇"<<endl;
system("pause");system("cls");
MainMenu();
break;
}
}
void Display_text() //顯示本文內容
{
int i=0;
char c,ch[150];
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
while(infile.get(c))
{
cout<<c;
i++;
if(i>=1000&&c=='\n'||i>=1500)
{
cout<<endl<<endl;
system("pause");system("cls");
i=0;
}
}
infile.close();
SetColor(11,8);
cout<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
system("pause");system("cls");
MainMenu();
}
void Count_ch() //統計字元數,空格數,段落數
{
int i=0,j=0,k=0;
char c,ch[150];
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
while(infile.get(c))
{
i++;
if(c==' ')j++;
if(c=='\n')k++;
}
infile.close();
SetColor(11,8);
cout<<"字元數:"<<i<<endl;
cout<<"空字元數"<<j<<endl;
cout<<"段落數(回車次數)"<<k<<endl;
SetColor(7,0);
system("pause");system("cls");
MainMenu();
}
void Search_KeyWord() //檢索關鍵字
{
int i=0,j=0,k=0;
char c,ch[150],kw[50];
SqString s,t;
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
cout<<"請輸入關鍵字:";
cin>>kw;
cout<<endl;
while(infile.get(c)&&i<MaxSize)
{
s.data[i]=c;i++;
}
s.len=i;
infile.close();
for(i=0;kw[i]!='\0';i++)
t.data[i]=kw[i];
t.len=i;
index(s,t);
if(tag[0]==-1)cout<<"無此關鍵字"<<endl;
else
{
for(i=0;i<=s.len;i++)
{
if(i==tag[j])
{
for(;i<tag[j]+t.len;i++)
{
SetColor(10,8);
cout<<s.data[i];
SetColor(7,0);
}
j++;
}
else cout<<s.data[i];
k++;
if(k>=1500&&s.data[i]=='\n'||k>=2000)
{
cout<<endl<<endl;
system("pause");system("cls");
k=0;
}
}
SetColor(11,8);
cout<<endl<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
}
system("pause");system("cls");
MainMenu();
}
void Replace_KeyWord() //替換關鍵字
{
int i=0,j=0,k=0;
char c,ch[150],kw[50],nkw[50];
SqString s,t,nt;
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
cout<<"請輸入關鍵字:";
cin>>kw;
cout<<endl;
while(infile.get(c)&&i<MaxSize)
{
s.data[i]=c;i++;
}
s.len=i;
infile.close();
for(i=0;kw[i]!='\0';i++)
t.data[i]=kw[i];
t.len=i;
index(s,t);
if(tag[0]==-1)cout<<"無此關鍵字"<<endl;
else
{
cout<<"請輸入新的字元替代原關鍵字:"<<endl;
cin>>nkw;
for(i=0;nkw[i]!='\0';i++)
nt.data[i]=nkw[i];
nt.len=i;
for(i=0;i<=s.len;i++)
{
if(i==tag[j])
{
for(int n=0;i<tag[j]+nt.len;i++,n++)
{
s.data[i]=nt.data[n];
SetColor(10,8);
cout<<s.data[i];
SetColor(7,0);
}
j++;
}
else cout<<s.data[i];
k++;
if(k>=1500&&s.data[i]=='\n'||k>=2000)
{
cout<<endl<<endl;
system("pause");system("cls");
k=0;
}
}
SetColor(11,8);
cout<<endl<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
}
fstream outfile(ch,ios::out);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
for(i=0;i<=s.len;i++)
{
outfile<<s.data[i];
}
outfile.close();
system("pause");system("cls");
MainMenu();
}
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor) //顏色函數
{
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
void index(SqString s,SqString t) //簡單匹配演算法(BF)
{
int i=0,j=0,k=0;
h0: while(i<s.len&&j<t.len)
{
if(s.data[i]==t.data[j])
{
i++;j++;
}
else
{
i=i-j+1;j=0;
}
}
while(j>=t.len)
{
tag[k]=i-t.len;
k++;
i++;j=0;
goto h0;
}
if(k==0)tag[0]=-1;
}
⑺ C語言編程,文本替換,學的東西比較少,想了很久也做不出來,求幫忙……
假設要被替換的文件名為1.txt,替換文本為2.txt,且它們都在當前目錄下,用兩行代碼就可以解決:
remove("1.txt");
rename("2.txt","1.txt");
⑻ c語言中,如何實現程序替換功能
#include<stdio.h>
#include<string.h>
void main(){
int i;
char str[100]="We will rock you";
printf("去掉空格,然後小寫轉大寫:");
for(i=0;i<strlen(str);i++){
if((int)str[i]>=97&&(int)str[i]<=122){
str[i] = (char)((int)str[i]-32); //注意小寫字母的ASCII碼是在97-122之間,而大寫字母的ASCII碼是在65-90之間,對應的字母相差32
}
if((int)str[i]!=32){ //空格的ASCII碼是32,去掉空格,肯定是不輸出空格
printf("%c",str[i]);
}
}
printf("\n");
printf("保留空格,然後小寫轉大寫:");
for(i=0;i<strlen(str);i++){
if((int)str[i]>=97&&(int)str[i]<=122){
str[i] = (char)((int)str[i]-32);
}
printf("%c",str[i]); //轉成大寫字母後 ,原樣輸出(字母是大寫但是帶空格的)
}
printf("\n");
}
⑼ c語言編程替換文件中字元串
方法和詳細的操作步驟如下:
1、第一步,依次單擊visual C ++ 6.0的「文件」-->「新建」-->「文件」-->「C++ Source File」選項,見下圖,轉到下面的步驟。
⑽ 在 C語言中字元串的替換如何實現的!
#include <stdio.h>
#include <string.h>
#include <malloc.h>
// 將strRes中的t替換為s,替換成功返回1,否則返回0。
int StrReplace(char strRes[],char from[], char to[]) {
int i,flag = 0;
char *p,*q,*ts;
for(i = 0; strRes[i]; ++i) {
if(strRes[i] == from[0]) {
p = strRes + i;
q = from;
while(*q && (*p++ == *q++));
if(*q == '\0') {
ts = (char *)malloc(strlen(strRes) + 1);
strcpy(ts,p);
strRes[i] = '\0';
strcat(strRes,to);
strcat(strRes,ts);
free(ts);
flag = 1;
}
}
}
return flag;
}
int main() {
char str[80] = "098123asd098opu";
printf("替換前:%s\n",str);
if(StrReplace(str,"098","33210")) printf("替換後:%s\n",str);
else printf("沒有任何替換。\n");
return 0;
}