当前位置:首页 » 编程语言 » c语言编程中添加变成替换
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言编程中添加变成替换

发布时间: 2022-10-07 07:33:17

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("abcd", "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;
}