Created
June 9, 2011 18:59
-
-
Save lefty313/1017444 to your computer and use it in GitHub Desktop.
string_manipulation
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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
char * create_char_vector(int i) | |
{ | |
char *vector; | |
vector = (char *) calloc(i + 1,sizeof(char *)); | |
vector[i+1] = 0; | |
return vector; | |
} | |
void print_char_vector(char * array) | |
{ | |
int i; | |
printf("\n"); | |
for (i = 0; i < strlen(array); ++i) printf("\%c",array[i]); | |
} | |
void free_char_array(char ** array) | |
{ | |
int i = 0; | |
do { | |
free(array); | |
++i; | |
} while (array[i]); | |
free(array); | |
} | |
void print_char_array(char ** array) | |
{ | |
int i = 0; | |
do | |
{ | |
printf("\n%s",array[i]); | |
++i; | |
} while (array[i]); | |
} | |
char * string_upcase(char *vector) | |
{ | |
int i; | |
char *result_vector = create_char_vector(strlen(vector)+1); | |
for(i = 0; i < strlen(vector)+1; ++i) result_vector[i] = toupper(vector[i]); | |
return result_vector; | |
} | |
char * string_downcase( char *vector) | |
{ | |
int i; | |
char *result_vector = create_char_vector(strlen(vector)); | |
for(i = 0; i < strlen(vector); ++i) result_vector[i] = tolower(vector[i]); | |
return result_vector; | |
} | |
char * string_reverse( char *vector) | |
{ | |
int i; | |
char *result_vector = create_char_vector(strlen(vector)); | |
for(i = 0; i < strlen(vector); ++i) result_vector[i] = vector[strlen(vector) - 1 - i]; | |
return result_vector; | |
} | |
int string_compare( char *vector_a, char *vector_b) | |
{ | |
int i; | |
char *temp1 = create_char_vector(strlen(vector_a)); | |
char *temp2 = create_char_vector(strlen(vector_b)); | |
temp1 = string_downcase(vector_a); | |
temp2 = string_downcase(vector_b); | |
i = strcmp(temp1,temp2); | |
free(temp1); | |
free(temp2); | |
return i; | |
} | |
char ** string_split(char *char_vector, char sign) | |
{ | |
char *string; | |
char *rest; | |
char *temp = char_vector; | |
char **array; | |
int i; | |
int count = 0; | |
for(i = 0; i < strlen(temp); ++i) | |
{ | |
if (temp[i] == sign) ++count; | |
} | |
array = (char **) calloc(count + 1, sizeof(char*)); | |
i = 0; | |
while(string = strtok_s(temp,&sign, &rest)) { | |
array[i] = create_char_vector(strlen(string)); | |
array[i] = string; | |
temp = rest; | |
++i; | |
} | |
array[count + 1] = 0; | |
return array; | |
} | |
void pstring_split(char *char_vector, char sign) | |
{ | |
char *string; | |
char *rest; | |
char *temp = char_vector; | |
int i; | |
i = 0; | |
while(string = strtok_s(temp,&sign, &rest)) { | |
print_char_vector(string); | |
temp = rest; | |
++i; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char text1[] = {"lubie duze literki\0"}; | |
char text2[] = {"ty tez jestes bogiem\0"}; | |
char text3[] = {"acnok do einm zsipan\0"}; | |
char text4[] = {"Ty Tez jeStes Bogiem\0"}; | |
char *string; | |
char **array; | |
char delimiter = ' '; | |
printf("string = %s",text1); | |
printf("\n\nstring_upcase(\"%s\") = ",text1); | |
string = string_upcase(text1); | |
printf("%s",string); | |
printf("\n\nstring_downcase(\"%s\") = ",string); | |
string = string_downcase(text1); | |
printf("%s",string); | |
printf("\n\nstring_reverse(\"%s\") = ",text3); | |
string = string_reverse(text3); | |
printf("%s",string); | |
printf("\n\nstrcmp(\"%s\", \"%s\") = ",text2,text4); | |
printf("%d",strcmp(text2,text4)); | |
printf("\n\nstring_compare(\"%s\", \"%s\") = ",text2,text4); | |
printf("%d",string_compare(text2,text4)); | |
printf("\n\nstring_split (\"%s\" ,\"%c\") = \n",text2,delimiter); | |
array = string_split(text3,delimiter); | |
print_char_array(array); | |
printf("\n\npstring_split (\"%s\" ,\"%c\") = \n",text4,delimiter); | |
pstring_split(text4,delimiter); | |
free(string); | |
//free_char_array(array); | |
getchar(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment