首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
admin
2019-03-29
88
问题
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。
选项
答案
/////////////////////////////////////////////////////////////////////// // Reverse a string between two pointers // Input: pBegin - the begin pointer in a string // pEnd - the end pointer in a string /////////////////////////////////////////////////////////////////////// void Reverse(char *pBegin, char *pEnd) { if(pBegin == NULL || pEnd == NULL) return; while(pBegin < pEnd) { char temp = *pBegin; *pBegin = *pEnd; *pEnd = temp; pBegin ++, pEnd --; } } /////////////////////////////////////////////////////////////////////// // Reverse the word order in a sentence, but maintain the character // order inside a word // Input: pData - the sentence to be reversed /////////////////////////////////////////////////////////////////////// char* ReverseSentence(char *pData) { if(pData == NULL) return NULL; char *pBegin = pData; char *pEnd = pData; while(*pEnd != ’\0’) pEnd ++; pEnd--; // Reverse the whole sentence Reverse(pBegin, pEnd); // Reverse every word in the sentence pBegin = pEnd = pData; while(*pBegin != ’\0’) { if(*pBegin == ’ ’) { pBegin ++; pEnd ++; continue; } // A word is between with pBegin and pEnd, reverse it else if(*pEnd == ’ ’ || *pEnd == ’\0’) { Reverse(pBegin, --pEnd); pBegin = ++pEnd; } else { pEnd ++; } } return pData; }
解析
由于编写字符串相关代码能够反映程序员的编程能力和编程习惯,与字符串相关的问题一直是程序员笔试、面试题的热门题目。本题也曾多次受到包括微软在内的大量公司的青睐。
由于本题需要翻转句子,我们先颠倒句子中的所有字符。这时,不但翻转了句子中单词的顺序,而且单词内字符也被翻转了。我们再颠倒每个单词内的字符。由于单词内的字符被翻转两次,因此顺序仍然和输入时的顺序保持一致。
还是以上面的输入为例子。翻转“I am a student.”中所有字符得到“.tneduts a ma I”,再翻转每个单词中字符的顺序得到“students. a am I”,正是符合要求的输出。
转载请注明原文地址:https://kaotiyun.com/show/IxmZ777K
0
程序员面试
相关试题推荐
TruthinadvertisingisaconceptcentraltotheAmericanfreemarketeconomicsystem.Accordingtothistheory,companiesthat
Writealettertothedirectorofthelibraryinyouruniversity,givingsomeadviceonhowtoimprovethelibraryservice.You
RememberNapsterorGrokster?Bothservicesalloweduserstosharecomputerfiles—usuallydigitalmusic—thatinfringedthecopyr
[A]Theperson-skillsmatchapproachtoselection[B]Theimpactsofbadselectiondecisions[C]Theimportanceofstructu
输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。例如输入整数22和如下二元树10
删除串中指定的字符
根据委托(delegate)的知识,请完成以下用户控件中代码片段的填写:namespacetest{publicdelegatevoidOnDBOperate();publicclassUserControlBase
在桌面上打开“我的电脑”窗口。
在“幻灯片浏览视图”模式下,不允许进行的操作是()A.幻灯片移动和复制B.幻灯片切换C.幻灯片删除D.设置动画效果
随机试题
Agreenhouseisabuildingmadeofglasswhichisusedforkeepingplantswarmwhentheoutsidetemperatureishow.Inasimilar
一病人,两天前出现腹痛泄泻,经治无效。现泄泻清稀,甚者如水样,腹痛肠鸣,脘闷纳少,苔薄白或白腻,脉濡数。应诊断为
新的建设用地审批制度设立了()、()、具体项目用地审批的相应程序,改变了过去单项征地审批的格局,旨在规范对建设用地的审批管理。
预警信息管理系统是集计算机技术与()为一体的智能化系统。
关于车辆购置税的计算,下列说法正确的有( )。
银行接受客户的委托,为工商企业办理与货币资本运动有关的技术性业务,如汇兑、非现金结算等,使银行成为企业的总会计、总出纳,成为社会的总账房,这是商业银行的()职能。
[*]
WhenthefirstwhitemanarrivedinSamoa,theyfoundblindmen,whocouldseewellenoughtode-scribethingsindetailjustb
把……比作……
Manypeoplewronglybelievethatwhenpeoplereacholdage,theirfamiliesplacetheminnursinghomes.Theyareleftinthe【C1】
最新回复
(
0
)