zoukankan      html  css  js  c++  java
  • 反转句子中单词的顺序(单词中字符的顺序保持不变,c语言)

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 void exchange(char *string, int c1, int c2);
     5 void revertString(char *string, int iStart, int iEnd);
     6 void revertWordsInString(char *string);
     7 
     8 int main(int argc, const char * argv[])
     9 {
    10 
    11     // insert code here...
    12     printf("Begin>>>!
    ");
    13     
    14     char originalString[100] = "abcdef";
    15     printf("%s
    ", originalString);
    16     size_t len = strlen(originalString);
    17     revertString(originalString, 0, (int)(len-1));
    18     printf("%s
    ", originalString);
    19     
    20     char string[100] = "I am a student coming from XX";
    21     printf("%s
    ", string);
    22     revertWordsInString(string);
    23     printf("%s
    ", string);
    24     
    25     
    26     return 0;
    27 }
    28 
    29 void revertString(char *string, int iStart, int iEnd)
    30 {
    31     while (iStart < iEnd) {
    32         exchange(string, iStart, iEnd);
    33         iStart++;
    34         iEnd--;
    35     }
    36 }
    37 
    38 void exchange(char *string, int c1, int c2)
    39 {
    40     char tmp = string[c1];
    41     string[c1] = string[c2];
    42     string[c2] = tmp;
    43 }
    44 
    45 void revertWordsInString(char *string)
    46 {
    47     size_t len = strlen(string);
    48     int indexEnd = (int)len - 1;
    49     int i = 0;
    50     int j = 0;
    51     while (j < indexEnd) {
    52         while (*(string + j) != ' ') {
    53             j++;
    54             if (j >= indexEnd) {
    55                 break;
    56             }
    57         }
    58         if (j < indexEnd) {
    59             revertString(string, i, j-1);
    60             j++;
    61             i = j;
    62         }
    63     }
    64     revertString(string, i, j);
    65     revertString(string, 0, indexEnd);
    66 }
  • 相关阅读:
    html5 postMessage解决跨域、跨窗口消息传递
    获取url参数值(可解码中文值)
    正则去除字符串中的html标签,但不去除<br>标签
    vue中watch检测到不到对象属性的变化的解决方法
    封装LocalStorage.js
    this.$router.push、replace、go的区别
    Javascript实现base64的加密解密
    堆排序
    归并排序
    单例模式
  • 原文地址:https://www.cnblogs.com/huangzizhu/p/3749844.html
Copyright © 2011-2022 走看看