#include <stdio.h> #include <stdlib.h> #include <string.h> /* 一道面试题,"i love china",倒序输出字符串,单词不倒序. 一开始搞错了思路,写应用多了就总往应用上靠.其实用指针遍历一遍就好. */ void outputWord(const char* str,int nlen); int main(void) { const char* str = "i love china"; unsigned int nlen = strlen(str); char* ptr = (char*)(str + nlen-1); int nLen4Word = 0; while (ptr != str-1) { nLen4Word++; if (*ptr == ' ') { outputWord(ptr+1,nLen4Word-1); printf(" "); nLen4Word = 0; } ptr--; } if (nLen4Word > 0) { outputWord(str,nLen4Word); } printf("\n"); return 0; } void outputWord(const char* str,int nlen) { //printf("str is %s,len is %d\n",str,nlen); for (int i = 0;i < nlen; i++) { printf("%c",str[i]); } }
转载请注明出处.
写博客的目的:记录,升华,量变到质变