文章标题
来源:互联网 发布:js清除div内容 编辑:程序博客网 时间:2024/04/30 03:49
c/字符串,字符转数字,数字转字符(转)
(2011-03-19 11:56:48)it
分类: 转载在C/C++语言中没有专门的字符串变量,通常用字符数组来存放字符串。字符串是以“\0”作为结束符。C/C++提供了丰富的字符串处理函数,下面列出了几个最常用的函数。
字符串是面试的重点考查部分的相关知识,通过考查字符串的相关知识可以考察程序员的编程规范以及编程习惯。并且其中包括了许多知识点,例如内存越界、指针与数组操作等。许多公司在面试时会要求应聘者写一段复制字符串或字符串子串操作的程序。本章列举了一些与字符串相关的面试题,有些题目要求较高的编程技巧。
6.1
应聘时经常出现数字与字符串之间转化的问题,面试官通过这类题目来考察应聘者能力,例如是否熟悉常用的库函数,是否了解ASCII码以及是否了解字符串的存储格式等。
6.1.1
面试例题1:使用库函数将数字转换为字符串。
考点:C库函数中数字转换为字符串的使用。
出现频率:★★★
解析
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明。
还可以使用sprintf系列函数把数字转换成字符串,其比itoa()系列函数运行速度慢。下列程序演示了如何使用itoa()函数和gcvt()函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
程序输出结果:
1
2
答案
可以使用atoi系列函数把数字转换成字符串。
面试例题2:不使用库函数将整数转换为字符串。
考点:数字转换为字符串,理解相关ASCII码。
出现频率:★★★★
解析
如果不使用atoi或sprintf等库函数,可以通过把整数的各位上的数字加“0”转换成char类型并存到字符数组中。但是要注意,需要采用字符串逆序的方法。如以下程序所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
程序中的int2str函数完成了int类型到字符串类型的转换。在代码第46行对int2str函数做了测试。程序的执行结果如下所示:
Please input an integer: 1234
Output: 1234
如果输入的是个负数,程序执行结果如下所示:
Please input an integer: -1234
Output: -1234
接下来对int2str函数的实现进行分析。
6.1.2
面试例题3:使用库函数将字符串转换为数字。
考点:C库函数中字符串转换为数字的使用。
出现频率:★★★★
解析
与上节数字转换为字符串类似,C/C++语言提供了几个标准库函数,可以将字符串转换为任意类型(整型、长整型、浮点型等)。以下列举了各函数的方法及其说明。
以下程序演示如何使用atoi ()函数和atof ()函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
输出结果:
num_int: 435
num_double: 436.550000
面试例题4:不使用库函数将字符串转换为数字。
考点:字符串转换为数字时,对相关ASCII码的理解。
出现频率:★★★★
解析
程序代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
程序执行结果:
输入:1234
输出:1234
输入:-1234
输出:-1234
输入:+1234
输出:1234
程序中的str2int函数作用是将字符串转换成整数。这个函数的转换过程与例题2中的int2str函数相比更加简单,它只需要做一次while循环(代码第13行)就能把数值大小计算出来,如果结果是负数,就加一个负号。
C++字符串,数字相互转换
一.将CString转为CTime的几种方法
CString
——–or - ———————
—- or ————————
CString
从上面来看,很明显使用sscanf()函数的优势.
二.将CTIme转换为CString的方法:
CTime
CString szTime = tmScan.Format(“’%Y-%m-%d%H:%M:%S’”);
这样得到的日期时间字符串就是以”2006-11-2723:30:59”的格式.这是不是很方便呢?
http://blog.csdn.net/steedhorse/archive/2005/03/25/330206.aspx
time_t t = time(0);
char s[32];
strftime(s, sizeof(s), “%Y-%m-%d %H:%M:%S”,localtime(&t));
sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,这一对由于从面向对象哪里得到了赞助,用以写出的代码更觉优雅。
三,字符串转换为数值类型
将字符串”20.0E6”转换为数字
1,sscanf(“20.0e5”,”%d”,&x);
2,atof(“20.0E6”);
许多人用atoi(), atof() 和这个“家族”中的其它函数. 它们方便应用,但是有一个重要的缺点:
在转换失败和转换字符串”0”时都返回0, 这样使得一致性错误检查变得几乎不可能。 为了完整性我们给出了小段代码:
代码:
——————————————————————————–
——————————————————————————–
一个更好的办法:
更有一点复杂, 更遗一致的办法是利用sscanf()
代码:
——————————————————————————–
——————————————————————————–
Since sscanf() takes a const char* parameter, you can directlyuse a CString with it:
因为sscanf()用const char* 作为参数, 所以你可以直接用CString作参数:
代码:
——————————————————————————–
——————————————————————————–
小心格式描述符(如本例中的”%d”)。sscanf()没有办法检查格式描述符与传递变量的类型匹配与否。如果不匹配你将得到不可预期的结果。同样注意sscanf()可以一次从字符串中提取一个或多个数值。 详细信息请查阅MSDN。
C++ 方法
如下的例子展示了利用标准C++类的来完成这个任务的模板函数
代码:
——————————————————————————–
#include <sstream>
#include <iostream>
template <class T>
bool from_string(T &t,
{
}
int main()
{
}
四,
1。 int<->CString
1) int ->CString
int n = 1;
CString str;
str.Format(“%d”,n);
2) CString->int
CString str = “1”;
int n = atoi(str.GetBuffer(0));
2. char* 与CString
1)char*->CString
char sz[128];
CString str;
str.Format(“%s”,sz);
2) CString -> char*
CString str;
//int nLength = str.GetLength();
char* sz = str.GetBuffer(0);
3.float<->CString
1)float->CString
float f = 0.0;
CString str;
str.Format(“%f”,f);
2) CString->float
CString str = “0.0”;
float f = atof(str.GetBuffer(0));
</div> <div class="clearit"></div> </div> <div class="articalInfo"> <!-- 分享到微博 {$t_blog} --> <div class="IL"> 阅读<span id="r_4c8a2a870100qgq7" class="SG_txtb">(12534)</span><em class="SG_txtb">┊</em> <a href="#commonComment">评论</a> <span id="c_4c8a2a870100qgq7" class="SG_txtb">(0)</span><em class="SG_txtb">┊</em> <a href="javascript:;" onclick="$articleManage('4c8a2a870100qgq7',5);return false;">收藏</a><span id="f_4c8a2a870100qgq7" class="SG_txtb">(0)</span> <em class="SG_txtb">┊</em><a href="#" id="quote_set_sign" onclick="return false ;">转载</a><a href="#" id="z_4c8a2a870100qgq7" onclick="return false ;" class="zznum">(19)</a> <span id="fn_c/字符串,字符转数字,数字转字符(转)" class="SG_txtb"></span><em class="SG_txtb">┊</em> <a onclick="return false;" href="javascript:;"><cite id="d1_digg_4c8a2a870100qgq7">喜欢</cite></a><a id="d1_digg_down_4c8a2a870100qgq7" href="javascript:;"><b>▼</b></a> <em class="SG_txtb">┊</em><a href="http://blog.sina.com.cn/main_v5/ria/print.html?blog_id=blog_4c8a2a870100qgq7" target="_blank">打印</a><em class="SG_txtb">┊</em><a id="q_4c8a2a870100qgq7" onclick="report('4c8a2a870100qgq7');return false;" href="#">举报</a> </div> <div class="IR"> <table> <tbody><tr> <th class="SG_txtb" scope="row">已投稿到:</th> <td> <div class="IR_list"> <span><img class="SG_icon SG_icon36" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" title="排行榜" align="absmiddle"> <a href="http://blog.sina.com.cn/lm/114/113/day.html" class="SG_linkb" target="_blank">排行榜</a></span> </div> </td> </tr> </tbody></table> </div> </div> <div class="clearit"></div> <div class="blogzz_zzlist borderc" id="blog_quote" style="display:none"><h3><a href="#" onclick="return false" title="关闭" id="ql_close4c8a2a870100qgq7" class="blogzz_closepic SG_floatR"></a>转载列表:</h3> <ul class="ul_zzlist" id="ql_content4c8a2a870100qgq7"> </ul> <ul style="display:none"><li id="ql_tip4c8a2a870100qgq7"></li></ul> <div class="SG_clearB"></div> <div class="blogzz_btn"> <a id="btnArticleQuote4c8a2a870100qgq7" href="#" onclick="scope.article_quote && scope.article_quote.check('4c8a2a870100qgq7');return false;" class="SG_aBtn SG_aBtn_ico SG_turn"><cite><img class="SG_icon SG_icon111" id="quoteList_quote4c8a2a870100qgq7" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" align="absmiddle">转载</cite></a> <p id="quoteDescription4c8a2a870100qgq7" class="SG_turntxt" style="display: none;">转载是分享博文的一种常用方式...</p> </div> <div id="ql_page4c8a2a870100qgq7" class="blogzz_paged"></div> <div class="clearit"></div></div> <div class="articalfrontback SG_j_linedot1 clearfix" id="new_nextprev_4c8a2a870100qgq7"> <div><span class="SG_txtb">前一篇:</span><a href="http://blog.sina.com.cn/s/blog_4c8a2a870100q21d.html">[转载]寒假后一半</a></div> <div><span class="SG_txtb">后一篇:</span><a href="http://blog.sina.com.cn/s/blog_4c8a2a870100qgs9.html">转:string to char*的转换</a></div> </div> <div class="clearit"></div> <div id="loginFollow"></div> <div class="allComm"> <div class="allCommTit"> <div class="SG_floatL"> <strong>评论</strong> <span id="commAd_1" style="display: inline-block;"> <span style="margin-left:15px; width:220px; display:inline-block;"><a target="_blank" href="http://blog.sina.com.cn/lm/8/2009/0325/105340.html">重要提示:警惕虚假中奖信息</a></span> </span> </div> <div class="SG_floatR"><a class="CP_a_fuc" href="#post">[<cite>发评论</cite>]</a></div> </div> <ul id="article_comment_list" class="SG_cmp_revert"><li><div class="noCommdate"><span class="SG_txtb">做第一个评论者吧! <img class="SG_icon SG_icon134" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="18" height="18" title="" align="absmiddle"><a href="#post">抢沙发>></a></span></div></li></ul> <div class="clearit"></div> <div class="myCommPages SG_j_linedot1"> <div class="SG_page" id="commentPaging" style="display:none;"> <ul class="SG_pages"> </ul> </div> <div class="clearit"></div> </div> <a name="post"></a> <div class="writeComm"> <div class="allCommTit"> <div class="SG_floatL"> <strong>发评论</strong> <span></span> </div> <div class="SG_floatR"></div> </div> <div class="wrCommTit"> <div class="SG_floatL" id="commentNick" style="display:none;"></div> </div> <div class="formTextarea"> <div style="float:left;" id="commonComment"> <iframe id="postCommentIframe" frameborder="0" style="border:1px solid #C7C7C7; height:158px;width:448px;maring-top:1px;background-color:white;" src="http://blog.sina.com.cn/main_v5/ria/blank2.html"></iframe> <textarea id="commentArea" tabindex="1" style="display:none;"></textarea> </div> <div id="mobileComment" style="float:left;display:none;"> <textarea id="mbCommentTa" style="width:438px;height:150px;border:1px solid #C7C7C7;line-height:18px;padding:5px;"></textarea> </div> <div class="faceblk" id="faceWrap"> <div id="smilesSortShow" class="faceline1"> <div class="facestyle" id="recomm_1490236093963"><a href="#" key="302"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/302-25.gif" alt="小新小浪" title="小新小浪"></a><a href="#" key="308"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/308-25.gif" alt="炮炮兵" title="炮炮兵"></a><a href="#" key="315"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/315-25.gif" alt="张富贵" title="张富贵"></a><a href="#" key="316"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/316-25.gif" alt="旺狗" title="旺狗"></a><a href="#" key="331"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/331-25.gif" alt="悠嘻猴" title="悠嘻猴"></a><a href="#" key="351"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/351-25.gif" alt="酷巴熊" title="酷巴熊"></a></div><span class="SG_more"><a href="#">更多>></a></span><div class="clearit"></div></div> <ul id="smilesRecommended" class="faceline01"><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0321EN00SIGT.gif" alt="就不买你" title="就不买你" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0320EN00SIGT.gif" alt="股市" title="股市" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0319EN00SIGT.gif" alt="发霉" title="发霉" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0318EN00SIGT.gif" alt="陈水边" title="陈水边" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0317EN00SIGT.gif" alt="裁员" title="裁员" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0316EN00SIGT.gif" alt="音乐" title="音乐" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0315EN00SIGT.gif" alt="贴你" title="贴你" height="50" width="50"></a></li><li><a href="#"><img src="http://www.sinaimg.cn/uc/myshow/blog/misc/gif/E___0314EN00SIGT.gif" alt="抢车位" title="抢车位" height="50" width="50"></a></li></ul> </div> <div class="clearit"></div> </div> <div class="formLogin"> <div class="SG_floatL"> <p id="commentlogin" style="display: block;"><span>登录名:</span><input type="text" style="width: 115px;" id="login_name" tabindex="2"> <span>密码:</span><input type="password" style="width: 115px;" id="login_pass" tabindex="3"> <a href="http://login.sina.com.cn/getpass.html" target="_blank">找回密码</a> <a href="http://login.sina.com.cn/signup/signup.php?entry=blog&src=blogicp&srcuid=1284123271" target="_blank">注册</a> <input type="checkbox" id="login_remember"><label for="login_remember" style="display:inline-block;" title="建议在网吧/公用电脑上取消该选项">记住登录状态</label></p><p id="commentloginM" style="display:none;"><span>昵 称:</span><input type="text" style="width: 115px;" id="comment_anonyous" value="新浪网友" tabindex="2" disabled=""></p><p id="quote_comment_p"><input type="checkbox" id="bb"> <label for="bb"><img height="18" align="absmiddle" width="18" title="" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" class="SG_icon SG_icon110">分享到微博 <img height="15" align="absmiddle" width="15" title="新" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" class="SG_icon SG_icon11"></label> <input type="checkbox" id="cbCommentQuote"><label for="cbCommentQuote">评论并转载此博文</label><img class="SG_icon SG_icon11" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="15" height="15" title="新" align="absmiddle"></p> <p id="geetest-box"><div id="geetest_1490236101775" class="gt_holder gt_float" style="touch-action: none;"><div class="gt_input"><input class="geetest_challenge" type="hidden" name="geetest_challenge"><input class="geetest_validate" type="hidden" name="geetest_validate"><input class="geetest_seccode" type="hidden" name="geetest_seccode"></div><div class="gt_slider"><div class="gt_guide_tip gt_show">按住左边滑块,拖动完成上方拼图</div><div class="gt_slider_knob gt_show" style="left: 0px;"></div><div class="gt_curtain_tip gt_hide">点击上图按钮并沿道路拖动到终点处</div><div class="gt_curtain_knob gt_hide">移动到此开始验证</div><div class="gt_ajax_tip gt_ready"></div></div></div></p> </div> <span style="display: none; color: rgb(153, 153, 153); margin-left: 10px;" id="login_remember_caution"></span> <div class="SG_floatR" id="anonymity_cont" style="display: none;"><input type="checkbox" id="anonymity"><label for="anonymity">匿名评论</label></div> </div> <div class="formBtn"> <a href="javascript:;" onclick="return false;" class="SG_aBtn" tabindex="5"><cite id="postcommentid">发评论</cite></a> <p class="SG_txtc">以上网友发言只代表其个人观点,不代表新浪网的观点或立场。</p> </div> </div> </div> <div class="clearit"></div> <div class="articalfrontback articalfrontback2 clearfix"> <div class="SG_floatL"><span class="SG_txtb">< 前一篇</span><a href="http://blog.sina.com.cn/s/blog_4c8a2a870100q21d.html">[转载]寒假后一半</a></div> <div class="SG_floatR"><span class="SG_txtb">后一篇 ></span><a href="http://blog.sina.com.cn/s/blog_4c8a2a870100qgs9.html">转:string to char*的转换</a></div> </div> <div class="clearit"></div></div>
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- CocoaPods安装和使用教程
- 嵌入式Linux系统---ppp拨号,4G模块上网
- 连接远程服务器上的mysql会出现的问题和解决方式(账号设置等操作)
- android使用groovy环境配置及所遇问题
- C语言用数组实现贪吃蛇
- 文章标题
- mysql如何创建数据库、创建表
- ThreadLocal 学习
- TreeSet遍历
- 参加WOT2017全球架构与运维技术峰会是一种什么体验
- [JQuery]window.onload和$(document).ready的区别
- 算法学习系列(贪心算法)—机器人攀登问题
- Android读取手机的联系人
- Andriod:缓存