Created
November 8, 2010 22:12
-
-
Save erenon/668366 to your computer and use it in GitHub Desktop.
pggyak-kzh-2.c
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 <stdlib.h> | |
#include <string.h> | |
char *trim(char *str) { | |
int i=0, spbefore=0, slen=0, spafter=0; | |
char *newstr; | |
slen = strlen(str); | |
while (str[i] == ' ') { | |
spbefore++; | |
i++; | |
} | |
i=slen-1; | |
while (str[i] == ' ') { | |
spafter++; | |
i--; | |
} | |
//slen-spbefore-spafter = a trimmelt lanc hossza | |
//+1 a lezaro 0-nak | |
newstr = (char *)malloc((slen-spbefore-spafter+1) * sizeof(char)); | |
if (newstr == NULL) { return NULL; } | |
for (i=0; i < slen-spafter-spbefore; i++) { | |
newstr[i] = str[i+spbefore]; | |
} | |
newstr[i] = 0; | |
return newstr; | |
} | |
int main() { | |
char *trimmed; | |
trimmed = trim(" foobarRRR "); | |
printf("|%s|\n", trimmed); | |
free(trimmed); | |
trimmed = NULL; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment