• 倒序打印非数字开头的可视字符串


    #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;
    }
    
  • 相关阅读:
    js正则
    常用正则表达式
    JS
    Vue
    JS
    Cookie、Session和自定义分页
    ORM分组操作示例(与SQL语句的比较)以及基于对象和queryset的正反查询
    跨站请求伪造和csrf_token使用
    ORM之单表、多表操作
    Django中ORM介绍和字段及字段参数
  • 原文地址:https://www.cnblogs.com/4thirteen2one/p/15316367.html
Copyright © 2020-2023  润新知