字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“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”.的更多相关文章

  1. 匹配字符串中的s开头的单词,并替换

    String s="now it's sping,but today is so cold!"; String a=s.replaceAll("s\\w+",& ...

  2. [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 ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  9. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

随机推荐

  1. Thinkphp分布式数据库连接代码分析

    Thinkphp作为国内的一款流行框架,相信使用的人一定不在少数.本篇我们来分析一下Thinkphp中比较重要的一部分——分布式数据库的连接. 当然了,我们在这里不是去将如何使用模型去对数据库进行增删 ...

  2. Java Socket实战之一 单线程通信基础socket

    现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用spring,其中就可以支持很多种远程连接的操作,另外jboss的remoting也是不错的选择,还有Apache ...

  3. 010.MySQL-Keepalived搭配脚本04

    vim /etc/keepalived/check_MySQL.sh #!/bin/bash pkill keepalived

  4. git初级浅入其常用操作

    1. git init 我们从初始化一个仓库开始,通过此命令可以初始化一个仓库 git init 首先我们在当前目录下创建一个目录pratice和一个文件test.js mkdir pratice c ...

  5. JAVA 图形开发之计算器设计(事件监听机制)

    /*文章中用到的代码只是一部分,需要源码的可通过邮箱联系我 1978702969@qq.com*/ 前段时间刚帮同学用MFC写了个计算器,现在学到JAVA的图形开发,就试着水了一个计算器出来.(可以说 ...

  6. UML用例图之间的关系

    在画用例图的时候,理清用例之间的关系是重点.用例的关系有泛化(generalization).扩展(extend)和包含(include).其中include和extend最易混淆.下面我们结合实例彻 ...

  7. Java日期和时间

    目录 Java 日期和时间 Java 日期和时间 学习自 http://blog.csdn.net/zjf280441589/article/details/50447533 Date类 Date 类 ...

  8. 关于操作Access数据库jdk选择问题

    关于操作Access数据库,使用jdk64位无法通过ODBC无法获取数据,只能通过jdk32位进行开发.

  9. 钻牛角尖还是走进死胡同--shell脚本根据名称获得 dubbo 服务的 pid

    到了下午,突然觉得坐立不安,可能是因为中午没有休息好.老大不小了还在做页面整合的事情,这是参加工作时就干的工作了.然后突然想去挑战高级一点的缺陷排查,结果一不小心就钻了一个牛角尖.启动 dubbo 服 ...

  10. oracle 根据约束名查表名

    PK_ID为约束名 select constraint_name,constraint_type,table_name from all_constraints where CONSTRAINT_NA ...