zoukankan      html  css  js  c++  java
  • 倒序打印非数字开头的可视字符串

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 65535
    // char *fgets(char *buf, int bufsize, FILE *stream);
    
    void reverseWord(char* b, char* e) {
        while(e-b > 0) {
            char tmp = *b;
            *(b++) = *e;
            *(e--) = tmp;
        }
    }
    
    int main() {
        char s[MAX];
        fgets(s, MAX, stdin);
        // char *fgets(char *buf, int bufsize, FILE *stream);
        int slen = strlen(s)-1;
        //including the '
    ', so need minus 1
    
        int splen = 0;
        //as it is
        for(char* ps = s; ps-s < slen; ps++) {
            //get the indexes of blank spaces
            if (*ps == ' ') {
                splen += 1;
            }
        }
        printf("Space counts: %d
    ", splen);
    
        int idx[splen];
        //indexes of blank space
        int* pi = idx;
        for(char* ps = s; ps-s < slen; ps++) {
            if (*ps == ' ') {
                *(pi++) = ps-s;
            }
        }
    
        char* last = s-1;   //上一个空格
        char* this = s-1;
    
        for (int* pi = idx; pi-idx < splen; pi++) {
            this = s + *pi;     //当前空格的位置
            if(this > s) {      //第一个字符不是空格
                char* b = last + 1;
                char* e = this - 1;
                if(*b < '0' || *b > '9')
                    reverseWord(b, e);
            }
            last = this;    //做记号,上一个空格在哪
            if(pi-idx == splen-1 && this < s+slen-1) {
                this = s + slen;
                char* b = last + 1;
                char* e = this - 1;
                if(*b < '0' || *b > '9')
                    reverseWord(b, e);
            }
        }
    
        fputs(s, stdout);
    
        return 0;
    }
    
  • 相关阅读:
    Javascript继承,再谈
    RHEL7使用systemctl管理服务
    centos7进入单用户模式修改root密码
    IBM DS5020 管理口密码重置
    IBM小机拆镜像换盘
    HMC版本支持
    IBM产品系列和AIX系统版本
    AIX 6.1创建逻辑卷并挂载【smitty】
    AIX中的网络管理
    创建AIX克隆盘
  • 原文地址:https://www.cnblogs.com/4thirteen2one/p/15316367.html
Copyright © 2011-2022 走看看