1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

97 lines
2.3 KiB

#include <stdio.h>
#include <time.h>
#include <string.h>
// #define LOGGING 1
// #define log_var(string, content) if (LOGGING) {time_t current_time; time(&current_time); printf("%s",asctime(localtime(&current_time))); printf(string, content);}
// #define log_str(string) if (LOGGING) {time_t current_time; time(&current_time); puts(asctime(localtime(&current_time))); puts(string);}
void enumerate_string(char test_array[]) {
char *ptr;
ptr = &(test_array[0]);
for (int i=0; i<5; i++) {
printf("%c", *(ptr + i));
}
}
int copy_str(char * out_string, char * string) {
/* A drop in replacement for strcpy() from <string.h>*/
int len = 0;
while (*(string + len) != '\0') {
*(out_string + len) = *(string + len);
len++;
}
*(out_string + len) = '\0';
return (1);
}
int get_str_len(char * string) {
int len = 0;
while (*(string + len) != '\0') {
len++;
}
return(len);
}
void replace_char(char * array, char * charac) {
int length;
length = sizeof(*array)/sizeof(char);
char combined[length];
int i = 0;
int counter = 0;
while (*(array + i) != '\0') {
if (*(array + i) != *charac) {
*(array + counter) = *(array + i);
counter++;
}
i++;
}
*(array + counter + 1) = '\0';
}
void join_strings(char * return_val, char * str_1, char * str_2) {
int len1 = get_str_len(str_1);
int len2 = get_str_len(str_2);
int length = len1 + len2;
char combined[length+1];
int i = 0;
int j = 0;
while (*(str_1 + i) != '\0') {
combined[i] = *(str_1 + i);
i++;
}
while (*(str_2 + j) != '\0') {
combined[len1+j] = *(str_2 + j);
j++;
}
copy_str(return_val, combined);
}
void get_time(void) {
char time_str[100];
time_t current_time;
time(&current_time);
copy_str(time_str, asctime(localtime(&current_time)));
strcat(time_str, "hello world");
//replace_char(&(time_str[0]), "\n");
printf("%s", time_str);
char string[20];
copy_str(string, "NONEWLINEhello");
replace_char(&(time_str[0]), "\n");
char newstr[120];
join_strings(&(newstr[0]), &(time_str[0]), &(string[0]));
replace_char(&(newstr[0]), "\n");
printf("%s", newstr);
}