字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”.
字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”. #include<iostream>
#include<stdio.h>
void Reverse(char *pb,char *pe)
{
if(pb==NULL||pe==NULL)
return;
while(pb<pe)
{
char tmp=*pb;
*pb=*pe;
*pe=tmp;
pb++,pe--;
}
} char *ReverseSentence(char *pData)
{
if(pData==NULL)
return NULL;
char *pBegin=pData;
char *pEnd=pData;
while(*pEnd!='\0')
pEnd++;
pEnd--;
Reverse(pBegin,pEnd);
pBegin=pEnd=pData;
while(*pBegin!='\0')
{
if(*pBegin==' ')
{
pBegin++;
pEnd++;
continue;
}
else if(*pEnd==' '||*pEnd=='\0')
{
Reverse(pBegin,--pEnd);
pBegin=++pEnd;
}
else
pEnd++; }
// printf("%s",pData);
return pData;
} int main()
{
char str[]="Today is Friday!"; //在主函数中传入调用函数的值必须是字符数组类型的值
char *str1; //而不能使指向字符串的指针,否则被调用函数无法访问字符串。
printf("源字符串为:%s\n",str);
str1=ReverseSentence(str);
while(str1!='\0')
{
std::cout<<*str1;
str1++;
}
// std::cout<<std::endl;
return ;
} 若指针指向一个字符串,这个字符串是保存在数据段常量区的,是不可以修改的。但我们可以让这个指针指向其他的字符串。
但是所示数组保存字符串的话,是存在栈区的,数组又是常量指针,即数组的这地址是不可以修改的,所以上面程序不会修改字符串的值。
要想字符指针像字符数组一样使用,需要提前申请相应的的内存空间,并在使用完以后对他进行释放。
字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”.的更多相关文章
- 匹配字符串中的s开头的单词,并替换
String s="now it's sping,but today is so cold!"; String a=s.replaceAll("s\\w+",& ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] Bold Words in String 字符串中的加粗单词
Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any le ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- [Swift]LeetCode434. 字符串中的单词数 | Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- [Swift]LeetCode557. 反转字符串中的单词 III | Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
随机推荐
- 出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误
实际上就是tomcat没有配置的原因 先去http://tomcat.apache.org 下载tomcat 然后根据http://jingyan.baidu.com/article/8065f87f ...
- miniblink+golang开发windows gui应用
golang的优点自不必说了,这么好的语言怎么能缺少界面库呢?所以我使用miniblink开发了一个可以用html,css,js开发界面的浏览器,通过它你能为你的golang开发简单的界面.说白了其实 ...
- sorted()排序详解
sorted()排序详解 http://wiki.python.org/moin/HowTo/Sorting?highlight=%28howto%29#The_Old_Way_Using_t ...
- 013.Zabbix的Items(监控项)
一 Items简介 Items是从主机里面获取的所有数据,可以配置获取监控数据的方式.取值的数据类型.获取数值的间隔.历史数据保存时间.趋势数据保存时间.监控key的分组等. 通常情况下item由ke ...
- Android通知栏沉浸式/透明化完整解决方案
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6640649.html 参考文献:https://github.com/ljgsonx/adaptiveSt ...
- iOS 11开发教程(十五)iOS11应用视图的位置和大小
iOS 11开发教程(十五)iOS11应用视图的位置和大小 当一个视图使用拖动的方式添加到主视图后,它的位置和大小可以使用拖动的方式进行设置,也可以使用尺寸检查器面板中的内容进行设置,如图1.52所示 ...
- BZOJ.1003.[ZJOI2006]物流运输(DP 最短路Dijkstra)
题目链接 容易看出是个最短路+DP.既然答案和天数有关,那么就令\(f[i]\)表示前\(i\)天最小成本. 这个转移很好想: \(f[i]=\min(f[i],\ f[j]+cost(j+1,i)+ ...
- 【BZOJ-3730】震波 动态点分治 + 树状数组
3730: 震波 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 626 Solved: 149[Submit][Status][Discuss] D ...
- BZOJ2905: 背单词 AC自动机+fail树+线段树
$zjq$神犇一眼看出$AC$自动机 $Orz$ 直接就讲做法了 首先对每个串建出$AC$自动机 将$fail$树找到 然后求出$dfs$序 我们发现一个单词 $S_i$是$S_j$的子串当且仅当$S ...
- 某题目2 状压DP
Description 对于一个数列,其混乱度定义为连续相等的数的段数.如:1 2 1 2 1,其混乱度为5,而:1 2 2 3 3,其混乱度为3.现给出一个数列,允许取出k个数并允许插入数列中的任意 ...