中文逗号改为英文逗号

daixuefeng203 2009-09-13 01:10:34
在程序字符串中如何将中文逗号改为英文逗号。

char* strHead="南昌,武汉,上海";
if (strstr(strHead,","))
{
cout<<"有中文逗号输入"<<endl;
//若有中文逗号输入则将其替换为英文逗号
char* p;
p=strstr(strHead,",");
*p=',';//这句话为什么老错?
*(p+1)=' ';

cout<<strHead<<endl;

...全文
1047 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
daixuefeng203 2009-09-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 thy38 的回复:]
引用 8 楼 daixuefeng203 的回复:
C语言也能用STL的string么?感谢你的回答呵呵


我晕,你发在C++区啊!你也没说只能用C不是?
[/Quote]
我的意思是你能用C语言写一个么?是我没说清楚,呵呵,我的程序本来就就写的不伦不类。
jean7155 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mstlq 的回复:]
请勿试图修改字符常量区的内容,可行代码如下……
C/C++ codechar strHead[]="南昌,武汉,上海";if (strstr(strHead,","))
{
cout<<"有中文逗号输入"<<endl;//若有中文逗号输入则将其替换为英文逗号char* p;
p=strstr(strHead,",");*p=',';//这句话为什么老错?*(p+1)='';

cout<<strHead<<endl;
[/Quote]

自己就想不到,一看答案就明白,我还是不行啊。
fbmsyu 2009-09-16
  • 打赏
  • 举报
回复
请发到c语言区,c++不推荐用c风格字符串.
thy38 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 daixuefeng203 的回复:]
C语言也能用STL的string么?感谢你的回答呵呵
[/Quote]

我晕,你发在C++区啊!你也没说只能用C不是?
daixuefeng203 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljx87085210 的回复:]
引用 1 楼 mstlq 的回复:
请勿试图修改字符常量区的内容,可行代码如下……
C/C++ codechar strHead[]="南昌,武汉,上海";if (strstr(strHead,","))
{
cout < <"有中文逗号输入" < <endl;//若有中文逗号输入则将其替换为英文逗号char* p;
p=strstr(strHead,",");*p=',';//这句话为什么老错?*(p+1)='';

cout < <strHead < <endl;

我没看出来有什么变化啊
[/Quote]
我看到了,我犯了个低级错误,概念没搞清楚
daixuefeng203 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 thy38 的回复:]
这里面有宽字符,如果不用w_char的话,就得用字符串来替换:
C/C++ code#include<iostream>
#include<string>int main()
{usingnamespace std;string strHead="南昌,武汉,上海";
cout<<"原字符串为:"<<strHead<<endl;if(strHead.find(",")!=string::npos) {
cout<<"有中文逗号输入"<<endl;
size_t it;while((it=strHead.find(","))!=string::npos) {
strHead.replace(it,2,",");
}
cout<<"替换后的结果为:"<<strHead<<endl;
}return0;
}
[/Quote]

C语言也能用STL的string么?感谢你的回答呵呵
thy38 2009-09-13
  • 打赏
  • 举报
回复
这里面有宽字符,如果不用w_char的话,就得用字符串来替换:
#include <iostream>
#include <string>

int main()
{
using namespace std;

string strHead="南昌,武汉,上海";
cout <<"原字符串为:"<<strHead<<endl;
if(strHead.find(",")!=string::npos) {
cout<<"有中文逗号输入"<<endl;
size_t it;
while((it=strHead.find(","))!=string::npos) {
strHead.replace(it, 2, ",");
}
cout<<"替换后的结果为:"<<strHead<<endl;
}

return 0;
}
kondykuang 2009-09-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljx87085210 的回复:]
引用 1 楼 mstlq 的回复:
请勿试图修改字符常量区的内容,可行代码如下……
C/C++ codechar strHead[]="南昌,武汉,上海";if (strstr(strHead,","))
{
cout < <"有中文逗号输入" < <endl;//若有中文逗号输入则将其替换为英文逗号char* p;
p=strstr(strHead,",");*p=',';//这句话为什么老错?*(p+1)='';

cout < <strHead < <endl;

我没看出来有什么变化啊
[/Quote]

他说 你程序中p=strstr(strHead,",");*p=',';//这句话为什么老错?*(p+1)='';
是有问题的,不能修改常量字符串
ljx87085210 2009-09-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mstlq 的回复:]
请勿试图修改字符常量区的内容,可行代码如下……
C/C++ codechar strHead[]="南昌,武汉,上海";if (strstr(strHead,","))
{
cout<<"有中文逗号输入"<<endl;//若有中文逗号输入则将其替换为英文逗号char* p;
p=strstr(strHead,",");*p=',';//这句话为什么老错?*(p+1)='';

cout<<strHead<<endl;
[/Quote]
我没看出来有什么变化啊
qingkongyihe2008 2009-09-13
  • 打赏
  • 举报
回复
引号中为常量,不可更改,需分开单独处理
yaobingxing 2009-09-13
  • 打赏
  • 举报
回复
*(p+1)=' ';
-------------
括号半角。
dclchj 2009-09-13
  • 打赏
  • 举报
回复
char* strHead="南昌,武汉,上海";
常量,不可以更改。
mstlq 2009-09-13
  • 打赏
  • 举报
回复
请勿试图修改字符常量区的内容,可行代码如下……
char strHead[]="南昌,武汉,上海";
if (strstr(strHead,","))
{
cout<<"有中文逗号输入"<<endl;
//若有中文逗号输入则将其替换为英文逗号
char* p;
p=strstr(strHead,",");
*p=',';//这句话为什么老错?
*(p+1)=' ';

cout<<strHead<<endl;

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧