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

想去除括号,这种方法怎么不对?请教?谁有更好、更快的办法?
...全文
231 7 打赏 收藏 转发到动态 举报
写回复
用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函数是判断非空格、非数字和非英文字母而已,还有其他的
第2章 数据类型 35 2.1 常用类型 36 0084 CString的多种初始化方式 36 0085 Windows字符串指针类型分析 36 0086 BSTR类型与CString类型的相互转换 36 0087 _bstr_与其他字符串类型的相互转换 37 0088 _VARIANT类型的初始化 37 0089 _variant_t与字符串类型的转换 38 0090 ColeVariant与其他数据类型的转换 38 0091 格式化类型 39 2.2 类型转换 39 0092 CString和char*之间的转换 39 0093 char与char*类型应用 39 0094 COLORREF类型转换RGB分量 40 0095 DWORD与 WORD之间的转换 40 0096 WORD与BYTE之间的转换 40 0097 字符串数值转换 40 2.3 字符串截取与转换 41 0098 获取字符串中的中文 41 0099 英文字符串首字母大写 41 0100 指定符号分割字符串 42 0101 在文本中删除指定的中文或中文句子 43 0102 替换指定的字符串 44 0103 向字符串中添加子字符串 44 0104 截取字符串中的数字 45 0105 将选定字符转换成大写 46 0106 截取指定位置的字符串 47 2.4 字符串的比较与判断 48 0107 判断字符中是否有中文 48 0108 判断字符串是否可以转换成整数 48 0109 如何判断字符串是否含有数字 49 0110 判断指定位置字符的大小写 49 0111 判断字符串中是否有指定的字符 50 0112 两个字符串进行比较 50 0113 在进行字符串比较时忽略大小写 50 0114 获取字符串中的英文子字符串 51 2.5 字符串技巧 52 0115 字符串加密 52 0116 字符串连接 52 0117 如何在字符串中使用双引号 53 0118 如何在字符串中添加多个空格 53 0119 字符反转 53 0120 去除字符首尾空格 53 0121 去除字符串中所有的空格 53 2.6 字符串应用 54 0122 将选定内容复制到剪贴板 54 0123 在ListBox中查找的字符串 54 0124 编辑框的行数,以及回车换行的数量 55 0125 字符串数组搜索 55 0126 设置编码方式 56 2.7 字符串统计 56 0127 如何统计中文个数 56 0128 获取字符串中数字位置 56 0129 获取字符在字符串中最后出现的位置 57 0130 获取大写字符的位置 58 0131 统计字符个数 59 0132 获取字符在字符串中出现的位置 60 0133 获取字符在字符串中出现的次数 61 0134 获取指定字符起始位置 62 0135 获取字符串中英文字母个数 63

33,311

社区成员

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

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