Last active
June 4, 2020 19:23
-
-
Save heatblazer/4004983472a2cf0a0fc42ad0cf912c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
void stlrot(char* str, int n); | |
void strrot(char* str, int n); | |
const char* strstrn(const char* orig, const char* pat, int n); | |
char* cutoff(const char *str, int from , int to); | |
const char* cuttoff2(const char* str, int from, int to); | |
int main(int argc, char* argv[]) | |
{ | |
(void)argc; | |
(void)argv; | |
char srev1[] = "123456789A"; | |
char srev2[] = "123456789A"; | |
char s1[] = "123456789A123456789B123456789C"; | |
int i = 0; | |
// const char* p = strstrn(s1, "123", 2); | |
for(i=0; i < 5; i++) { | |
stlrot(srev1, 1); | |
strrot(srev2, 1); | |
} | |
} | |
void stlrot(char* str, int n) | |
{ // return s[1:] + s[:1] | |
char* it = str; | |
char* s1=cutoff(str, n, strlen(str)); | |
char* s2=cutoff(str, 0, n); | |
char* t1 = s1; | |
char* t2 = s2; | |
if (!s1 || !s2) return; | |
while (*s1 != '\0') | |
*it++ = *s1++; | |
while(*s2 != '\0') | |
*it++ = *s2++; | |
free(t1); free(t2); | |
printf("%s\r\n", str); | |
} | |
void strrot(char* str, int n) | |
{ | |
// return s[strlen(str)-n:strlen(str)] + s[0:strlen(str)-n] | |
char* it = str; | |
char* s1=cutoff(str, 0, strlen(str)-n); | |
char* s2=cutoff(str, strlen(str)-n, strlen(str)); | |
char* t1 = s1; | |
char* t2 = s2; | |
if (!s1 || !s2) return; | |
while (*s2 != '\0') | |
*it++ = *s2++; | |
while(*s1 != '\0') | |
*it++ = *s1++; | |
free(t1); free(t2); | |
printf("%s\r\n", str); | |
} | |
const char* cuttoff2(const char* str, int from, int to) | |
{ | |
return NULL; | |
} | |
char* cutoff(const char* str, int from , int to) | |
{ | |
if (from >= to) | |
return NULL; | |
char* cut = calloc(sizeof(char), (to - from) + 1); | |
char* begin = cut; | |
if (!cut) | |
return NULL; | |
const char* fromit = str+from; | |
const char* toit = str+to; | |
(void)toit; | |
memcpy(cut, fromit, to); | |
return begin; | |
} | |
const char* strstrn(const char* orig, const char* pat, int n) | |
{ | |
const char* it = orig; | |
do | |
{ | |
const char* tmp = it; | |
const char* tmp2 = pat; | |
if (*tmp == *tmp2) { | |
while (*tmp == *tmp2 && *tmp != '\0') { | |
tmp++; | |
tmp2++; | |
} | |
if (n-- == 0) | |
return it; | |
} | |
tmp = it; | |
tmp2 = pat; | |
} while (*it++ != '\0'); | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment