首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student. a am I”。
admin
2014-11-15
53
问题
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。
选项
答案
由于本题需要翻转句子,我们先颠倒句子中的所有字符。这时,不但翻转了句子中单词的顺序,而且单词内字符也被翻转了。我们再颠倒每个单词内的字符。由于单词内的字符被翻转两次,因此顺序仍然和输入时的顺序保持一致。 还是以上面的输入为例子。翻转“I am a student.”中所有字符得到“.tneduts a ma I”,再翻转每个单词中字符的顺序得到“students. 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; }
解析
转载请注明原文地址:https://jikaoti.com/ti/Zag7FFFM
0
程序员面试
相关试题推荐
Theimmunesystemisequalincomplexitytothecombinedintricaciesofthebrainandnervoussystem.Thesuccessoftheimmune
Writeanessaybasedonthefollowingoutline.Youshouldwriteabout150wordsontheANSWERSHEET.1.教师用课外时间给学生补课赚钱的现象
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
删除字符串中的数字并压缩字符串(神州数码以前笔试题),如字符串”abc123de4fg56”处理后变为”abcdefg”。注意空间和效率。(下面的算法只需要一次遍历,不需要开辟新空间,时间复杂度为O(N))
.net中读写数据库需要用到哪些类?他们的作用
两个单向链表,找出它们的第一个公共结点。链表的结点定义为:structListNode{intm_nKey;ListNode*m_pNext;};
存储过程和函数的区别
从“系统属性”出发安装网卡驱动程序。
若按下Shift键同时用鼠标分别单击幻灯片中的标题、文本和图片对象,则()。A.仅最后单击的对象被选中B.三个对象被同时选中C.仅标题对象被选中D.仅图形对象被选中
随机试题
智力的二因素论,将智力分为普通因素和特殊因素,首先提出这一理论的是()
男性,55岁。出现黄疸、发热、呕吐,查体可见皮肤黏膜出血点。该患者可能是
秦皮的主要成分是洋金花的主要化学成分是
能够引起灰婴综合征的药物是()。
某银行以某公司未偿还贷款为由向法院起诉,法院终审判决认定其请求已过诉讼时效,予以驳回。某银行向某县政府发函,要求某县政府落实某公司的还款责任。某县政府复函:“请贵行继续依法主张债权,我们将配合做好有关工作。”尔后,某银行向法院起诉,请求某县政府履行职责。法
玻璃钢衬里中,在设备表面处理后,为了防止返锈而涂覆的涂层是()。
犯罪后自动投案是认定为自首的必要条件。下列情形中,应当视为自动投案的有()。
班级日常管理有哪些内容?()
地球表面的绿色植物通过光合作用,将太阳的辐射能量转化成能被其他生物利用的有机化合能。下面说法错误的是()。
TheAsian(1)hastakenitstollonHongKong’stouristindustry,(2)offoreignexchangeforthearea.VOAHongKongcorrespond
最新回复
(
0
)