Last active
September 23, 2016 08:26
-
-
Save KillerGoldFisch/5642509 to your computer and use it in GitHub Desktop.
Revisions
-
KillerGoldFisch revised this gist
May 24, 2013 . 1 changed file with 31 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,34 @@ /***************************************************************************************************** * * Sample: * ======= *#include <stdio.h> *#include "StringFunctions.h" * * *int main(){ * char* orginal = "Hans hatte heute Wurst zum Frühstück"; * char* orginalFix; sFixUmlauts(orginalFix = sCopy(orginal)); //Umlaute werden für die Console Gefixt * char* tmp = sReplace(orginalFix, "Hans", "Hans-Peter"); * char* neu = sReplace(tmp, "Wurst", "Wurstbrot"); * * printf("%s\n", neu); * * free(orginalFix); * free(tmp); * free(neu); * * char** words = sSplit(orginal," "); * char* word; * while((word = *(words++)) != NULL) * printf("%s\n", word); * * getchar(); * return 0; *} *****************************************************************************************************/ #include <stdlib.h> -
KillerGoldFisch created this gist
May 24, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,183 @@ #include <stdlib.h> #define NULL 0 int sLen(char* s); int sIndexOf(char* s, char* pattern, int startpos); int sCountOf(char* s, char* pattern); int* sPositionsOf(char* s, char* pattern); char* sJoin(char* s1, char* s2); char* sSubString(char* s, int start, int len); void sPlaceIn(char* s, char* pattern, int pos); void sPlaceIn(char* s, char* pattern, int pos, int len); void sPlaceIn(char* s, char* pattern, int pos, int ppos, int len); char* sCopy(char* s); void sReplace(char* s, char oldchar, char newchar); char* sReplace(char* s, char* oldpattern, char* newpattern); char** sSplit(char* s, char* sep); void sFixUmlauts(char* s); int sLen(char* s){ int n = 0; while(s[n] != NULL){ n++; } return n; } int sIndexOf(char* s, char* pattern, int startpos){ int n = startpos < 0 ? 0 : startpos; while(s[n] != NULL ){ if(s[n] == pattern[0]) { int i = 0; while(s[n + i] == pattern[i]){ i++; if(pattern[i] == NULL) return n; if(s[n + i] == NULL) return -1; } } n++; } return -1; //Not found } int sCountOf(char* s, char* pattern){ int patternLen = sLen(pattern); int n = 0; int pos = - patternLen; while((pos = sIndexOf(s, pattern, pos + patternLen)) >= 0) n++ ; return n; } int* sPositionsOf(char* s, char* pattern){ int count = sCountOf(s, pattern); int patternLen = sLen(pattern); int* positions = (int*)malloc((count + 1) * sizeof(int)); positions[count] = -1; int pos = - patternLen; int n = 0; while((pos = sIndexOf(s, pattern, pos + patternLen)) >= 0) positions[n++] = pos; return positions; } char* sSubString(char* s, int start, int len){ char* subString = (char*)malloc((len + 1) * sizeof(char)); subString[len] = NULL; for(int n = 0; n < len ; n++) subString[n] = s[start + n]; return subString; } char* sJoin(char* s1, char* s2){ int s1Len = sLen(s1); int s2Len = sLen(s2); char* newString = (char*)malloc((s1Len + s2Len + 1) * sizeof(char)); newString[s1Len + s2Len] = NULL; for(int n = 0 ; n < s1Len + s2Len ; n++) newString[n] = n < s1Len ? s1[n] : s2[n - s1Len]; return newString; } void sPlaceIn(char* s, char* pattern, int pos){ int n = 0; while(pattern[n] != NULL){ s[pos + n] = pattern[n]; n++; } //return s; } void sPlaceIn(char* s, char* pattern, int pos, int len){ int n = 0; while(pattern[n] != NULL && n < len){ s[pos + n] = pattern[n]; n++; } //return s; } void sPlaceIn(char* s, char* pattern, int pos, int ppos, int len){ int n = 0; while(pattern[n + ppos] != NULL && n < len){ s[pos + n] = pattern[n + ppos]; n++; } //return s; } char* sCopy(char* s){ int Len = sLen(s); char* newstring = (char*)malloc((Len + 1) * sizeof(Len)); newstring[Len] = NULL; for(int n = 0 ; n < Len ; n++) newstring[n] = s[n]; return newstring; } void sReplace(char* s, char oldchar, char newchar){ int len = sLen(s); for(int n = 0 ; n < len ; n++) if(s[n] == oldchar) s[n] = newchar; } char* sReplace(char* s, char* oldpattern, char* newpattern){ int oldPatternLen = sLen(oldpattern); int newPatternLen = sLen(newpattern); int count = sCountOf(s, oldpattern); int oldLen = sLen(s); int delta = newPatternLen - oldPatternLen; int newLen = oldLen + count * delta; char* newstring = (char*)malloc((newLen + 1) * sizeof(char)); newstring[newLen] = NULL; int* positions = sPositionsOf(s, oldpattern); int lastPos = 0; int lastPosOld = 0; for(int n = 0 ; n < count ; n++) { int newPos = positions[n] + n * delta; sPlaceIn(newstring, s, lastPos, lastPosOld, positions[n] - lastPos); sPlaceIn(newstring, newpattern, positions[n] + n * delta); lastPos = newPos + newPatternLen; lastPosOld = positions[n] + oldPatternLen; } if(lastPos < newLen) sPlaceIn(newstring, s, lastPos, lastPosOld, oldLen - lastPosOld); return newstring; } char** sSplit(char* s, char* sep){ int count = sCountOf(s, sep) + 1; int sepLen = sLen(sep); int* positions = sPositionsOf(s, sep); char** data = (char**)malloc((count + 1) * sizeof(char*)); data[count] = NULL; int lastPos = 0; for(int n = 0 ; n < count - 1 ; n++) { int len = positions[n] - lastPos; data[n] = sSubString(s, lastPos, len); lastPos = positions[n] + sepLen; } data[count - 1] = sSubString(s, lastPos, sLen(s) -lastPos); return data; } char _umlbro[] = { 'Ä' , 'ä' , 'Ö' , 'ö' , 'Ü' , 'ü' , 'ß' }; char _umlfix[] = { 0x8E, 0x84, 0x99, 0x94, 0x9A, 0x81, 0xE1 }; void sFixUmlauts(char* s){ for(int n = 0 ; n < 7 ; n++) sReplace(s, _umlbro[n], _umlfix[n]); }