本文主要是介绍笔试题:把一串字符串中单词逆序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例如输入:
wang zheng jun
输出:
gnaw gnehz nuj
#include <stdio.h>
#include <string.h>
int main()
{char str[100] = {0};int len = 0;int count = 0;int index = 0;char ch = 0;int begin = 0, end = 0;int i = 0;int spaceNumbers = 0;/* 得到输入的字符串 */gets(str);len = strlen(str);for(index = 0; index < len; index++){if(str[index] == ' '){end = index;for(i = 0; i < count/2; i++){ch = str[begin+i];str[begin+i] = str[end-1-i];str[end-1-i] = ch;}begin = end+1;count = 0;spaceNumbers++;}/* 说明到字符串的结尾了 */if(index == len-1){end = len;for(i = 0; i < count/2; i++){ch = str[begin+i];str[begin+i] = str[end-1-i];str[end-1-i] = ch;}}count++;}printf("%s\n",str);return 0;
}
这篇关于笔试题:把一串字符串中单词逆序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!