您的位置:首页 > 其它

字符串的相关处理问题

2011-08-19 12:59 246 查看
itoa函数  

原型:char *itoa( int value, char *string,int radix) ,其中的字符指针的参数既可以是数组名字(自动转化为指向数组的指针),也可以是动态申请的内存的指针。代码如下:

使用声明的字符数组:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i = 12;
char p[10] = {0}; //数组声明的时候全部初始化为0即'\0'

itoa(i, p, 10);

cout<<p<<endl;

return 0;
}

动态申请字符数组:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i = 12;
char *p = new char[10];

itoa(i, p, 10);

cout<<p<<endl;

return 0;
}

空字符的问题

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i = 12;
char p [10];

itoa(i, p, 10);

p[9] = '\0';
cout<<p<<p[9]<<"dd"; //怎么跟空格一个效果,空格和空字符什么关系

cout<<p[9]<<endl;

return 0;
}

空字符和空格字符的比较:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i = 12;
char p [10];

itoa(i, p, 10);

p[9] = '\0';
cout<<p<<p[9]<<"dd";

if (' ' == 0) //空格字符
{
cout<<"aaaa"<<endl; //此处无输出
}
else if ('\0' == 0)
{
cout<<"dddd"<<endl; //此处有输出
}

return 0;
}

格式化输出到字符数组:

sprintf

sprint_s

待完善
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: