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. Intelli IDEA ultimate破解方法

    今天装了个Intelli IDEA,ultimate版本,使用网上方法破解了,破解方法参考网址  http://appcode.aliapp.com/idea.jsp Intelli IDEA有个vi ...

  2. JavaScript Patterns 4.5 Immediate Functions

    The immediate function pattern is a syntax that enables you to execute a function as soon as it is d ...

  3. linux 查看系统版本

    博客分类: linux LinuxRedHatDebianSuSE  几种查看linux版本信息的方法: uname -a cat /proc/version cat /etc/issue lsb_r ...

  4. SharePoint 指定配置数据库访问账户“域账户\用户”

    大家在安装sharepoint时都会遇到这个问题,域账户,什么是域账户哪?域账户简单理解就是网路账户,与本地账户不同,什么是域哪?域就是控制器. 一台Windows 计算机,它要么隶属于工作组,要么隶 ...

  5. Nde模块篇

    /*模块分为两种:原生模块和文件模块.原生模块即Node.js API提供的原生模块,原生模块在启动时已经被加载.文件模块即为动态加载模块,加载文件模块的工作主要由原生模块 module 来实现和完成 ...

  6. STM32启动文件选择说明

    图1. STM32F10xxx标准外设库体系结构先说这个问题,大家都知道,我们在选择使用哪些外围的的时候,是去更改从官方模版中拷贝过来的stm32f10x_conf.h文件的27-48行,把我们要用的 ...

  7. APACHE重写去除入口文件index.php

    下面我说下 apache 下 ,如何 去掉URL 里面的 index.php 例如: 你原来的路径是: localhost/index.php/index 改变后的路径是: localhost/ind ...

  8. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

  9. GCC源码自动编译-python脚本

    一.前言 目前因机器OS GCC版本太老,导致无法编译一些新版本软件,所以写了一个自动编译GCC的python脚本,操作系统是比较老的suse 10, 很多系统自动软件版本都很低,所以此脚本一般可适用 ...

  10. 【Android Demo】简单手机通讯录

    Android 系统给我们提供了访问通讯录的接口,通过接口获取通讯录信息.Adapter 与 View 的连接主要依靠 getView 这个方法返回我们需要的自定义 view. ListView 是 ...