Last active
August 31, 2024 22:15
-
-
Save felipsmartins/a5f7557f279abacc6e0859b36effab5b to your computer and use it in GitHub Desktop.
String functions - Reinventng the wheel
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 characters
#define _POSIX_C_SOURCE 199309L /* shall be >= 199309L */ | |
#include <time.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
// clear; gcc -Wall strings.c -o strings && ./strings | |
// WARNING: This code is not safe to use in real life. | |
int str_len(const char *string) { | |
int i = 0, count = 0; | |
char ch = string[0]; | |
while (ch != '\0') { | |
count++; | |
i++; | |
ch = string[i]; | |
} | |
return count; | |
} | |
// check indicating if needle is contained in haystack. | |
int str_contains(const char *haystack, const char *needle) { | |
// fast path | |
if (haystack == needle) | |
return 1; | |
int offset = 0; | |
const int chars_to_match = str_len(needle); | |
int chars_matched = 0; | |
char ch = haystack[0]; | |
int i = 0; | |
while (ch != '\0') { | |
if (ch == needle[offset]) { | |
offset++; | |
chars_matched++; | |
} else { | |
offset = 0; | |
chars_matched = 0; | |
} | |
if (chars_matched == chars_to_match) { | |
return 1; // contains | |
} | |
i++; | |
ch = haystack[i]; | |
} | |
return 0; | |
} | |
void str_reverse(char *string) { | |
const int len = str_len(string); | |
char reversed[len + 1]; | |
int index = 0; | |
for (int i = (len - 1); i >= 0; i--) { | |
reversed[index] = string[i]; | |
index++; | |
} | |
reversed[len] = '\0'; | |
printf("NEW STRING: %s\n", reversed); | |
} | |
char *str_reverse2(const char *string) { | |
const int len = str_len(string); | |
int buflen = len + 1; | |
char reversed[buflen]; | |
// NOTE: hard to free memory. | |
char *buffer = malloc(buflen); | |
int index = 0; | |
for (int i = (len - 1); i >= 0; i--) { | |
reversed[index] = string[i]; | |
index++; | |
} | |
int ret = snprintf(buffer, buflen, "%s", reversed); | |
printf("snprintf ret val: %d\n", ret); | |
// the folloing works but requires string.h header, not fun: strcpy(buffer, | |
// reversed); | |
return buffer; | |
} | |
int main() { | |
// char text[] = "my little program"; | |
// char search[] = "program"; | |
// const int found = str_contains(text, search); | |
// | |
// if (found == 1) { | |
// printf("SUCCESS! '%s' is in '%s'\n", search, text); | |
// } else { | |
// printf("NOT FOUND! '%s' is NOT in '%s'\n", search, text); | |
// } | |
char *reversed = str_reverse2("MAL"); | |
printf("reversed string: %s\n", reversed); | |
struct timespec reqtime = {6, 0}; | |
nanosleep(&reqtime, NULL); | |
free(reversed); | |
puts("printing after free() call..."); | |
struct timespec reqtime2 = {10, 0}; | |
int ret = nanosleep(&reqtime2, NULL); | |
printf("DONE! ret: %d\n", ret); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment