字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“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. 1044-Access denied for user 'root'@'%' to database 'lc_db'

    远程登录Linux中的MySQL时,如果直接在工具中创建数据库时,有可能出现下面图中这样的错误: 这种错误是在远程登录时造成的,如果直接在Linux中本地操作没有问题(在Linux中的MySQL下,通 ...

  2. ps -aux与ps -ef

    ps -aux与ps -ef这两个命令显示的结果是差不多的. 不同之处就是显示风格不同,前者是BSD风格,后者SYSTEM V(其实我不太明白这尼玛风格是什么跟什么,我看起来都差不多啊) 然后重要的不 ...

  3. Garph Coloring

    题意:给了一个有 n 个点 m 条边的无向图,要求用黑.白两种色给图中顶点涂色,相邻的两个顶点不能涂成黑色,求最多能有多少顶点涂成黑色.图中最多有 100 个点 该题是求最大独立集团  最大团点的数量 ...

  4. 008 jquery过滤选择器-----------(子元素过滤选择器)

    1.介紹 2.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  5. 在 Vim 中优雅地查找和替换

    原文更好看链接http://harttle.com/2016/08/08/vim-search-in-file.html 总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细 ...

  6. CSS选择器优先级(转)

    原文:http://www.cnblogs.com/wangfupeng1988/p/4285251.html 另外,w3c有文章介绍了CSS选择器的特定性,见https://www.w3.org/T ...

  7. ADNI以及study design简介

    相关名词: MCI:轻度认知功能障碍 EMCI:早期认知障碍 MCI:轻度认知障碍 LMCI:晚期认知障碍 CN:认知正常的志愿者 DTI:doppler tissue imaging,多普勒组织显像 ...

  8. Linux-C网络编程

    简介 基础是TCP/IP协议,网上资料很多不再赘述. 推荐<图解TCP/IP> socket编程 网络字节序 发送主机通常将发送缓冲区中的数据按内存地址从低到高的顺序发出, 接收主机把从网 ...

  9. [USACO3.2]Sweet Butter

    题目大意: 给定一张$k$个结点,$m$条边的无向图,其中有$n$个点被标记,在这$k$个点中找出一个点使得这个点到那$n$个点的最短距离之和最小,求出这个距离和. 思路: 对于每个标记结点跑最短路, ...

  10. 2016 Multi-University Training Contest 6 题解

    我只能说: A Boring Question 下面公式重复了一行 \[ \sum\_{0\leq k\_{1},k\_{2},\cdots k\_{m}\leq n}\prod\_{1\leq j& ...