Added implementation for string length Added implementation for string copy Added implementation for string concatination
61 lines
985 B
C
61 lines
985 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int x_strlen(char* str){
|
|
char* ptr = str;
|
|
while(*ptr != '\0'){
|
|
ptr++;
|
|
}
|
|
return ptr - str;
|
|
}
|
|
|
|
void x_strcpy(char* dest, char* src, const int size_dest){
|
|
char* dest_ptr = dest;
|
|
char* src_ptr = src;
|
|
int i = 0;
|
|
|
|
while(*src_ptr != '\0'){
|
|
if (i >= size_dest){
|
|
fprintf(stderr, "Error: x_strcpy\nSource string exeeds destination memory\n");
|
|
exit(1);
|
|
}
|
|
*dest_ptr = *src_ptr;
|
|
dest_ptr++;
|
|
src_ptr++;
|
|
i++;
|
|
}
|
|
|
|
}
|
|
|
|
char* x_strconcat(char* str1, char* str2){
|
|
|
|
char* new_string;
|
|
|
|
int len1 = x_strlen(str1);
|
|
int len2 = x_strlen(str2);
|
|
int new_length = len1 + len2;
|
|
|
|
new_string = malloc( sizeof(*new_string) * new_length );
|
|
x_strcpy(new_string, str1, new_length);
|
|
x_strcpy(&new_string[len1], str2, new_length - len1);
|
|
|
|
return new_string;
|
|
}
|
|
|
|
int main(){
|
|
|
|
char x[] = "hlo";
|
|
char y[] = "world";
|
|
|
|
//printf("%d\n", x_strlen(x));
|
|
//printf("%d\n", x_strlen(x));
|
|
|
|
char* z = x_strconcat(x,y);
|
|
|
|
puts(z);
|
|
|
|
//free(z);
|
|
|
|
return 0;
|
|
}
|