C++char问题

lvchakele 2013-09-23 09:49:35
char *str = "青年杂志(上半年)";
char str[] = "青年杂志(上半年)";


如上两种,怎么才能把字符串中字符一个一个显示出来?

...全文
226 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jkkwhite 2013-09-25
  • 打赏
  • 举报
回复
如果在cout<<Temp;后面加cout<<" "; 为什么会乱码
Falleyes 2013-09-24
  • 打赏
  • 举报
回复
引用 2 楼 u011729265 的回复:

int main()
{
	char *str = "青年杂志(上半年)";
	char str1[] = "青年杂志(上半年)";
	int nLen = strlen(str);
	char Temp[4];

	for (int i=0; i<nLen; i=i+2)
	{
		memcpy(Temp, str+i, 2);
		Temp[2]='\0';

		cout << Temp;
	}
	cout << endl;

	for (int j=0; j<nLen; j=j+2)
	{
		memcpy(Temp, str1+j, 2);
		Temp[2]='\0';
		
		cout << Temp;
	}

	cout << endl;


	cout << str << "," << str1 << endl;
	
	return 0;
}
在VC6下,测试没问题。
+1
赵4老师 2013-09-24
  • 打赏
  • 举报
回复
_mbsnextc, _strnextc, _wcsnextc unsigned int _mbsnextc( const unsigned char *string ); Routine Required Header Compatibility _mbsnextc <mbstring.h> Win 95, Win NT _strnextc <tchar.h> Win 95, Win NT _wcsnextc <tchar.h> 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 Each of these functions returns the integer value of the next character in string. Parameter string Source string Remarks The _mbsnextc function returns the integer value of the next multibyte-character in string, without advancing the string pointer. _mbsnextc recognizes multibyte-character sequences according to the multibyte code page currently in use. The generic-text function _tcsnextc, defined in TCHAR.H, maps to _mbsnextc if _MBCS has been defined, or to _wcsnextc if _UNICODE has been defined. Otherwise _tcsnextc maps to _strnextc. _strnextc and _wcsnextc are single-byte–character string and wide-character string versions of _mbsnextc. _wcsnextc returns the integer value of the next wide character in string; _strnextc returns the integer value of the next single-byte character in string. _strnextc and _wcsnextc are provided only for this mapping and should not be used otherwise. For more information, see Using Generic-Text Mappings and Appendix B, Generic-Text Mappings. String Manipulation Routines See Also _mbsdec, _mbsinc, _mbsninc
军说网事 2013-09-23
  • 打赏
  • 举报
回复

int main()
{
	char *str = "青年杂志(上半年)";
	char str1[] = "青年杂志(上半年)";
	int nLen = strlen(str);
	char Temp[4];

	for (int i=0; i<nLen; i=i+2)
	{
		memcpy(Temp, str+i, 2);
		Temp[2]='\0';

		cout << Temp;
	}
	cout << endl;

	for (int j=0; j<nLen; j=j+2)
	{
		memcpy(Temp, str1+j, 2);
		Temp[2]='\0';
		
		cout << Temp;
	}

	cout << endl;


	cout << str << "," << str1 << endl;
	
	return 0;
}
在VC6下,测试没问题。
aizibion 2013-09-23
  • 打赏
  • 举报
回复
转成unicode,两个字节两个字节的打印

33,311

社区成员

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

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