将一串字符(hello world I am c_player)反转为(c_player am I world hello)
来源:互联网 发布:(综琼瑶)知君用心 编辑:程序博客网 时间:2024/06/06 00:11
hello world I am c_player >> c_player am I world hello
如果将转换后的字符串中的每个单词进行反转得到
reyalp_c ma I dlrow olleh
可以看到此时的字符串正好是原字符串整体的反转由此可以得到思路
先将原字符串中每个单词进行反转,然后对其整体在进行反转,便可以得到目标串。
代码:
void work(char *str, int len){ char *p, *q; p = str; q = str + len - 1; //指向单词末尾 while((p-q) < 0) { char tmp ; tmp = *p; *p = *q; *q = tmp; p++; q--; }}void fun(char *str){ int len = strlen(str); char *p = str; char *tmp; tmp = p; int i; while(*p != '\0') // 对出去最后一个单词外的单词进行反转 { if(*p == ' ') { i = p - tmp; work(tmp,i); //进行反转的函数 tmp = p + 1; } p++; } work(tmp,p-tmp); //对最后一个单词进行反转 work(str,len); //对整个字符串进行反转}int main(int ac, char **av){ char a[100] = "hello world I am c_player"; printf("a[]:%s\n",a); fun(a); printf("later:%s\n",a);}
效果
0 0
- 将一串字符(hello world I am c_player)反转为(c_player am I world hello)
- hello! I am vten.
- hello i am comin
- everyone I am Hello everyone I am
- hello java, i am rookie
- 将字符串student a am i反转为 i am a student
- 将“hello world” 输出为“world hello”
- Tell the world I am coming home
- Hello,Dream!I am On The Way!
- Hello,world!I'm coming
- 一个字符数组的内容为:"student a am i", 请你将数组的内容改为"i am a student"
- 浪潮面试题(将一个句子中的单词反转)例:i am bob 输出 bob am i
- 将字符串【"Hello world" 】 倒序结果为【"world Hello " 】
- ios之第一个程序Hello word, I am chenyu !
- hello everybody, my name is harmful, i am so excite.
- 创建 Windows8 应用 Part I: Hello, world!
- I. Writing the "Hello world" program
- AutoCAD I/O - 第二篇(Hello World)
- Android 打包获取release版apk
- HDU 1195Open the Lock(bfs)
- 走进数据结构之排序(二)---希尔排序
- hadoop作业执行之TaskTracker
- Mybatis三剑客(mybatis-generator、mybatis-plugin、mybatis-pagehelper)介绍、配置及破解
- 将一串字符(hello world I am c_player)反转为(c_player am I world hello)
- Linux常用命令大全
- ES6学习——新的语法:Symbol private类属性
- C、C++中的结构体
- mysql多实例
- BrocastReceiver(二)
- Tomcat部署项目,访问的时候不带项目名称
- LB/LVS之KeepAlived总结
- 网络缓冲图片到sd卡