思路分析:采用hash法来实现,首先申请一个长度为256的表,对每个字符hash计数即可。因为C/C++中的字符有3种类型:char、signedchar和unsignedchar。char类型的符号是由编译器指定的,一般是有符号的。在对字符进行hash时,应该先将字符转为无符号类型;否则当下标为负值时,就会出现越界访问。此外,还需要一个数组记录当前找到的只出现一次的字符,避免对原字符串进行第二次遍历。代码如下:#include "stdafx.h"#include char GetChar(char str[]){ if (str == NULL) return 0; . Read More
posted @ 2014-03-24 22:30 源子陌 Views(613) Comments(0) Diggs(0)
方法一:先求出字符串长度,然后反向遍历。代码如下:#include "stdafx.h"#include void ReversePrint(const char* s){ int len = strlen(s); for (int i = len - 1; i >= 0; i--) printf("%c", s[i]); }int main(){ char a[] = "abcd"; ReversePrint(a); printf("\n"); getchar(); return 0;} 效果如图: 方法二 Read More
posted @ 2014-03-24 16:34 源子陌 Views(631) Comments(0) Diggs(0)
思路分析:一共分两个步骤,第一步先按单词逆序,第二步将整个句子逆序。代码如下:#include "stdafx.h"#include void ReverseWord(char* p, char* q){ while (p < q) { char t = *p; *p = *q; *q = t; p++; q--; }}char* Reverse(char *s){ char *p = s; char *q = s; while (*q != '\0') { if (*... Read More
posted @ 2014-03-24 16:26 源子陌 Views(431) Comments(0) Diggs(0)
方法一:普通逆序。直接分配一个与愿字符串登场的字符数组,然后反向拷贝即可。代码如下:#include "stdafx.h"#include char *Reverse(char *s){ char *q = s; while (*q)q++; q=q-1; char *p = new char[sizeof(char)*(q - s + 2)]; char *r = p; //逆序存储 while (q >= s) { *p = *q; p++; q--; } *p = '\0'; re... Read More
posted @ 2014-03-24 11:24 源子陌 Views(1234) Comments(0) Diggs(0)