倒排句子的单词

来源:互联网 发布:小白素材vip源码 编辑:程序博客网 时间:2024/04/30 11:24
/*以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中*/#include<iostream>using namespace std;void reverse_str(char *str, int n){char *pbegin = str;char *pend = str + n - 1;while(pbegin < pend){char tmp = *pbegin; *pbegin = *pend;   *pend = tmp;   pbegin++;   pend--;}}void replace_to_blank(char *str){while (*str++) {if (',' == *str || '.' == *str || '?' == *str){*str = ' ';}}}void StrOL(char*str){replace_to_blank(str);char *phead = str; char *ptail = str+strlen(str);reverse_str(phead, strlen(str));char *pend = NULL;while (1){pend = strchr(phead, ' ');if (pend == 0) { pend = ptail;reverse_str(phead, pend - phead);break;}reverse_str(phead, pend - phead);phead = pend + 1;}} void main(){char str[100] = { "how are you doing? You He Me, I am a student."};StrOL(str);cout << str;system("pause");}

0 0
原创粉丝点击