以单词为单位,对字符串进行反转
2009-09-08 12:22
465 查看
要求以单词为单位进行字符串的反转,代码如下:
还有人家写的几个编程题:http://www.zhuxinquan.com/cn/cat-48/
还有人家写的几个编程题:http://www.zhuxinquan.com/cn/cat-48/
// WordReverse.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdlib.h" #include "string.h" char* CharReverse(const char*, int); char *WordReverse(const char *,int); int _tmain(int argc, _TCHAR* argv[]) { char * p = "I am student"; char * d = WordReverse(p,12); return 0; } // 以单词反转 char *WordReverse(const char *strSource,int strLen) { char *strWord = CharReverse(strSource, strLen); char *strDes = (char *) malloc(sizeof(char) * strLen); char *temp = strDes; int startPos = 0; // 记录单词开始反转的位置 for(int i = 0; i < strLen + 1; i++) { if( strWord[i] == ' ' | strWord[i] == '\n') //如果遇到空格反转上一个单词 { for(int j = i - 1; j >= startPos; j--) *strDes++ = strWord[j]; if(strWord[i] == '\n') //判断trWord最后一个单词 *strDes++ = '\n'; else *strDes++ = ' '; startPos = i + 1; } } return temp; } // Reverse the entire string char *CharReverse(const char * strSource, int strLen) { char *strDes = (char *) malloc(sizeof(char) * strLen); char * temp = strDes; for(int i = strLen -1; i >= 0; i--) { *strDes++ = strSource[i]; } *strDes++ = '\n'; return temp; }
相关文章推荐
- 以单词为单位,对字符串进行反转
- 字符串以单词为单位进行反转,并且必须保留原有分隔符及其个数
- 以单词为单位反转字符串,要求不申请任何空间
- php字符串按照单词进行反转的方法
- Python Cookbook 第二版 汉化版 [Recipe 1.7] 以单词或字符为单位对字符串进行反序排列
- 以单词为单位对字符串进行翻转(用C#编写,但不用其中的库函数)
- Python Cookbook 第二版 汉化版 [Recipe 1.7] 以单词或字符为单位对字符串进行反序排列
- php字符串按照单词进行反转的方法
- java将字符串进行反转单词需正确拼写
- 字符串单词反转
- 反转字符串中的单词
- 字符串中的所有单词进行倒排
- 42 - 反转单词的顺序 | 左旋转字符串
- 反转字符串中的单词
- 以行为单位对字符串变量下标为奇数位置上的字符按其ASCii值从小到大的顺序进行排序,排序后的结果仍按行重新存入字符串数组xx中
- 字符串中 单词反转
- 将字符串以单词为单位逆序"I am a Student" 解法
- 不用任何库函数实现以单词为单位的字符串逆转算法(C#)
- php实现按单词对字符串进行翻转
- 文章中单词个数统计 字符串以空格反转