Moves in squared strings (I)(C语言CodeWars)

解题思路:

(1)首先复制字符串,这样可以减少对"\n"的操作

(2)将看似二维的数,一维展开,在纸上找找感觉~

#include <stdio.h>
#include <stdlib.h>

char* horMirror(char* strng) {
    int tlen = strlen(strng),cols,begin,end;
    char *str = strdup(strng),*token = strtok(strng,"\n");
    cols = strlen(token),begin = 0,end = cols;
    while(token!=NULL) {
        for(int i=begin;i<end;i++) str[(tlen-cols)-begin+(i-begin)]=strng[i];
	begin = end+1,end = end+1+cols;
	token = strtok(NULL,"\n");
    }
    return str;
}

char* vertMirror(char* strng) {
    int tlen = strlen(strng),cols,begin,end;
    char *str = strdup(strng),*token = strtok(strng,"\n");
    cols = strlen(token),begin = 0,end = cols;
    while(token!=NULL) {
	for(int i=begin;i<end;i++) str[end-(i-begin+1)]=strng[i];
	begin = end+1,end = end+1+cols;
	token = strtok(NULL,"\n");
    }
    return str;
}

typedef char* (*generic_func_t) (char*);
char* oper(generic_func_t f, char* s) {
    return f(s);
}
发布了317 篇原创文章 · 获赞 279 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/105714425
今日推荐