1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string.

这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了。C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标直接访问啊等等,C语言实现的时候要注意首先要用一个while循环来找到最后一个字符,然后再往中间走。参见代码如下:

C++:

class Solution {
public:
void reverse(string &s) {
int left = , right = s.size() - ;
while (left < right) {
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
};

C

class Solution {
public:
void reverse(char *str) {
char *right = str;
if (str) {
while (*right) ++right;
--right;
while (str < right) {
char tmp = *str;
*str++ = *right;
*right-- = tmp;
}
}
}
};

[CareerCup] 1.2 Reverse String 翻转字符串的更多相关文章

  1. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  2. [LeetCode] 344. Reverse String 翻转字符串

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

  3. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  4. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

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

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

  6. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

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

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

  8. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  9. lintcode :Reverse Words in a String 翻转字符串

    题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...

随机推荐

  1. asp接收jquery post 中文乱码问题!

    这个问题的解决主要还是通过url编码对中文进行处理,在服务后台代码中,进行url解码处理. 但是问题来了,asp没有解码的内置函数,只有一个编码的内置函数UrlEncode,而用UrlEncode进行 ...

  2. svn conflict

    安装svn apt-get install subversion 当前两个人都更新版本为version1 A修改了monitor.txt文件 提交后版本为version2 B也修改了monitor.t ...

  3. FinanceJson

    FinanceJson, 对Json的包装.底层使用jackson实现. 1. 生成节点 (1)在某个路径下添加某个节点 FinanceJson financeJsonInfo = FinanceJs ...

  4. 【log4net】配置

    第一步在 AssemblyInfo  添加如下代码 第二步:在web.config添加如下代码: <log4net> <root> <level value=" ...

  5. ubuntu16.04下安装openssh-server报依赖错误的解决方法

    问题:系统重装后,安装和配置SSH,防火墙配置 #安装install openssh-server sudo apt install openssh-server -y 遇到问题: sudo apt ...

  6. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9169   Accep ...

  7. find命令中参数perm的用法

    按照文件权限模式用-perm选项,按文件权限模式来查找文件的话.最好使用八进制的权限表示法.如在当前目录下查找文件权限位为755的文件,即文件属主可以读.写.执行,其他用户可以读.执行的文件,可以用: ...

  8. 前端mock数据之mockjax和mockjson

    用处 在前后台共同进行一个项目的时候常会遇到一种情景, 后台定义好接口,前端按照接口进行开发, 当前端开发完成后台接口却还没有开发完成, 这个时候要进行接口测试, 只能等后台开发完成才能测试, 在这中 ...

  9. windows内核编程之常用数据结构

    1.返回状态 绝大部分的内核api返回值都是一个返回状态,也就是一个错误代码.这个类型为NTSTATUS.我们自己写的函数也大部分这样做. NTSTATUS MyFunction() { NTSTAT ...

  10. 递归:codevs 1251 括号

    codevs 1251 括号  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 计算乘法时,我们可以添加括号,来改变相乘的顺 ...