字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“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" 输 ...
随机推荐
- 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)
本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...
- 000 Jquery的Hello World程序
1.介绍Jquery 2.简介 3.新建一个静态项目,并粘贴jquery库 4.程序 <!DOCTYPE html> <html> <head> <meta ...
- 基于SOA的银行系统架构
Part-1 [简述] 1.通过引入面向服务架构(SOA),企业服务总线(ESB),适配器(Adapter)及面向构件等技术,尝试打造一个统一业务流程服务平台,实现面向流程的服务集成. 2.传统银行 ...
- 5,EasyNetQ-Send Receive
而发布/订阅和请求/响应模式是位置透明的,因为您不需要指定消息的消费者所在的位置,发送/接收模式专门用于通过命名队列进行通信. 它也不会对可以通过队列发送的消息的类型做任何假设. 这意味着您可以通过同 ...
- BeanUtils工具
什么是BeanUtils工具 BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品. BeanUtils工具一般可以方便javaBean的哪些操作? 1)b ...
- [ 转载 ] Java 构造代码块
https://blog.csdn.net/liyunxiangrxm/article/details/80244016
- Gunicorn设计部分的翻译
Design 关于Gunicorn架构的简要描述. Server Model Gunicorn是基于pre-fork(预启动,提前fork)的工作模式.这就意味着Gunicorn是由一个主进程来管理这 ...
- Code Forces 698A Vacations
题目描述 Vasya has nn days of vacations! So he decided to improve his IT skills and do sport. Vasya know ...
- Windows系列之(一):Windows10 上运行Ubuntu Bash
1. 前言 2016年4月6日,Windows 10 Insider Preview 发布的版本 14316,添加了Ubuntu Bash,在Windows上提供一个Linux环境,可以直接执行Lin ...
- Mac 10.12使用free命令(fish)
前提:要使用free命令,可以安装fish shell,然后在里面安装free插件. 安装fish brew install fish 安装free fish fisher free 使用 fish ...