C++字符串翻转操作
来源:互联网 发布:js数组remove和push 编辑:程序博客网 时间:2024/06/05 10:42
举例:
I love cpp
翻转后
cpp love I
算法: 先翻转整个句子,再翻转每个单词
#include <stdio.h>#include <string.h>void reverseInPlace( char *input, int start, int end ){ char *s = input + start; char *e = input + end; while ( s < e ) { char tmp = *s; *s = *e; *e = tmp; s++; e--; }}
void reverseSentence ( char sentence[] ){ reverseInPlace(sentence,0,strlen(sentence)-1); //翻转整个句子 int len = strlen(sentence); int start = 0; int end = 0; bool lastIsChar = false; char *tmp = sentence; while ( '\0' != *tmp ) { if ( *tmp != ' ' ) { if ( lastIsChar == false ) { lastIsChar = true; start = tmp - sentence; } } else if ( ' ' == *tmp ) { if ( lastIsChar == true ) { lastIsChar = false; end = tmp -sentence-1; reverseInPlace(sentence,start,end); } } tmp ++; } if ( lastIsChar == true ) { reverseInPlace(sentence,start,tmp-sentence-1); }}
int main(int argc,char *argv[]){ char sentence1[] = "I love csdn "; reverseSentence(sentence1); fprintf(stderr,"%s",sentence1); fprintf(stderr,"\n"); char sentence2[] = " but csdn don't love me"; reverseSentence(sentence2); fprintf(stderr,"%s",sentence2); return 0;}
- c操作翻转字符串
- c语言字符串-常用操作(3) 字符串 翻转
- C++字符串翻转操作
- 实现字符串翻转操作
- 字符串翻转操作
- C# 翻转字符串
- C语言字符串翻转
- bnu1075 翻转字符串 C语言版
- 字符串单词翻转---程序库---C
- C语言实现翻转字符串
- 按单词翻转字符串 C语言实现
- C\C++笔试题IV---翻转字符串
- c语言简单的字符串翻转
- [每天一题]翻转C-style 字符串
- CareerCup之1.2C风格字符串翻转
- CC150:翻转一个C风格的字符串
- 【c语言】实现翻转字符串函数reverse_string
- C语言递归实现字符串翻转
- 特此声明
- spring3+hibernate4+junit4单元测试
- 怎样写SQL语句可以提高数据库的性能
- Linux 信号通信 .
- iOS开发如何实现消息推送机制
- C++字符串翻转操作
- Linux 管道通信 .
- greenplum单机安装方法
- PHP:windows PHP配置问题合集
- 20130327-[转]讲完这个故事我就要嫁给别人
- mingw下linphone-android编译
- 整数划分
- c语言中 main()函数的参数说明
- Notification的图标大小