
展开全部
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string.h>
using namespace std;
void Replace(char *str, char findChar, char replaceChar)
{
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == findChar)
{
str[i] = replaceChar;
}
}
}
void main()
{
char a[1024] = "a|b|c|d|";
Replace(a,'|','&');
cout<<a<<endl;
cout << endl << "按任意键退出" << endl;
getch();
}
#include <fstream>
#include <conio.h>
#include <string.h>
using namespace std;
void Replace(char *str, char findChar, char replaceChar)
{
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == findChar)
{
str[i] = replaceChar;
}
}
}
void main()
{
char a[1024] = "a|b|c|d|";
Replace(a,'|','&');
cout<<a<<endl;
cout << endl << "按任意键退出" << endl;
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <algorithm>
#include <cstring>
replace(a, a+strlen(a)+1, '|', '&');
#include <cstring>
replace(a, a+strlen(a)+1, '|', '&');
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CString::Replace
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
返回值
返回被替换的字符数。如果这个字符串没有改变则返回零。
参数
chOld
要被chNew 替换的字符
chNew
要用来替换chOld 的字符。
lpszOld
一个指向字符串的指针,该字符串包含了要被lpszNew 替换的字符。
lpszNew
一个指向字符串的指针,该字符串包含了要用来替换lpszOld 的字符。
说明
此成员函数用一个字符替换另一个字符。函数的第一个原形在字符串中用chNew
现场替换chOld。函数的第二个原形用lpszNew 指定的字符串替换lpszOld 指定
的子串。
在替换之后,该字符串有可能增长或缩短;那是因为lpszNew 和lpszOld 的长度
不需要是相等的。两种版本形式都进行区分大小写的匹配。
代码示例:
CString s1="�v%a";
CString s2="c";
int word=s1.Replace("%a",s2);
cout<<s1<<endl;
新建工程写代码的时候,遇到错误,CString是没定义的类型,以为只要包含String.h就行了,结果还是报错,一查才知道应该包含“afx.h”,包含了“afx.h”后还是有错
Compiling...
CReplaceChar.cpp
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/CReplace.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
修改配置,“Project-Settings-General"里改成”Using MFC in a Static Library“即可。
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );
返回值
返回被替换的字符数。如果这个字符串没有改变则返回零。
参数
chOld
要被chNew 替换的字符
chNew
要用来替换chOld 的字符。
lpszOld
一个指向字符串的指针,该字符串包含了要被lpszNew 替换的字符。
lpszNew
一个指向字符串的指针,该字符串包含了要用来替换lpszOld 的字符。
说明
此成员函数用一个字符替换另一个字符。函数的第一个原形在字符串中用chNew
现场替换chOld。函数的第二个原形用lpszNew 指定的字符串替换lpszOld 指定
的子串。
在替换之后,该字符串有可能增长或缩短;那是因为lpszNew 和lpszOld 的长度
不需要是相等的。两种版本形式都进行区分大小写的匹配。
代码示例:
CString s1="�v%a";
CString s2="c";
int word=s1.Replace("%a",s2);
cout<<s1<<endl;
新建工程写代码的时候,遇到错误,CString是没定义的类型,以为只要包含String.h就行了,结果还是报错,一查才知道应该包含“afx.h”,包含了“afx.h”后还是有错
Compiling...
CReplaceChar.cpp
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/CReplace.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
修改配置,“Project-Settings-General"里改成”Using MFC in a Static Library“即可。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询