C++去除字符串符号问题

lvchakele 2013-09-22 04:57:01
char *str = "青年杂志(上半年)";

int i, j;
for (i = j = 0; str[i] != '\0'; i++)
{
if (!ispunct(str[i]))
{
str[j++] = str[i];
}
}
str[j] = '\0';
cout<<str<<end

想去除括号,这种方法怎么不对?请教?谁有更好、更快的办法?
...全文
237 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你吧char *str改成char str[]试试
  • 打赏
  • 举报
回复
在ANSI C标准中禁止对string literal作出修改。K&R C中对这一问题的说明是,试图修改字符串常量的行为是未定义的。
赵4老师 2013-09-23
  • 打赏
  • 举报
回复
#pragma comment(linker,"/SECTION:.rdata,RW")
//加这句可以让常量区可写,后果自负!
赵4老师 2013-09-22
  • 打赏
  • 举报
回复
ispunct, iswpunct int ispunct( int c ); int iswpunct( wint_t c ); Each of these routines returns true if c is a particular representation of a punctuation character. Routine Required Header Compatibility ispunct <ctype.h> ANSI, Win 95, Win NT iswpunct <ctype.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value ispunct returns a non-zero value for any printable character that is not a space character or a character for which isalnum is true. iswpunct returns a non-zero value for any printable wide character that is neither the space wide character nor a wide character for which iswalnum is true. Each of these routines returns 0 if c does not satisfy the test condition. The result of the test condition for the ispunct function depends on the LC_CTYPE category setting of the current locale; see setlocale for more information. For iswpunct, the result of the test condition is independent of locale. Parameter c Integer to test Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _istpunct ispunct _ismbcpunct iswpunct Character Classification Routines | Locale Routines | is, isw Function Overview
buyong 2013-09-22
  • 打赏
  • 举报
回复
std::string::erase? std::remove?
max_min_ 2013-09-22
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:

char *str = "青年杂志(上半年)";
 //字符串常量,不能修改它的值的,只是可读的空间
//ispunct函数是判断非空格、非数字和非英文字母而已,还有其他的
改成这样吧!可读可写

char str[] = "青年杂志(上半年)"
max_min_ 2013-09-22
  • 打赏
  • 举报
回复

char *str = "青年杂志(上半年)";
 //字符串常量,不能修改它的值的,只是可读的空间
//ispunct函数是判断非空格、非数字和非英文字母而已,还有其他的

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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