diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-12-05 18:16:30 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-12-05 18:16:30 -0500 |
commit | 61b4313d03a93f50c5cb9d7320f3db7c21caa9f3 (patch) | |
tree | ee033e83a8ef3d602c16534d2dce3cf61259d83c /strings.c | |
parent | 236ceeb996f490d24b54984ba0833f0d4606a3de (diff) | |
download | jdtalkc-61b4313d03a93f50c5cb9d7320f3db7c21caa9f3.tar.gz |
Improvements, and heavy optimization to string search
* Add error checking to all allocs
* Add general format checking
* Add acronym format checking
* Consolidate dictionary creation into different helper functions
* Add ability to get a random word of a type without scanning the entire dictionary
* Added function doc strings (incomplete)
Diffstat (limited to 'strings.c')
-rw-r--r-- | strings.c | 41 |
1 files changed, 38 insertions, 3 deletions
@@ -1,16 +1,26 @@ #include "jdtalk.h" +/** + * Change case of a character... sometimes + * @param s input string (modified) + * @return pointer to s + */ char *str_random_case(char *s) { size_t len; len = strlen(s); for (size_t i = 0; i < len; i++) { - if ((rand() % 100) >= 50) { + if ((random() % 100) >= 50) { s[i] = (char)toupper(s[i]); } } return s; } +/** + * Capitalize every other character + * @param s input string (modified) + * @return pointer to s + */ char *str_hill_case(char *s) { size_t len; len = strlen(s); @@ -22,6 +32,11 @@ char *str_hill_case(char *s) { return s; } +/** + * Translate characters to 1337 + * @param s input string + * @return pointer to local storage (don't free it) + */ char *str_leet(char *s) { size_t len; static char buf[OUTPUT_SIZE_MAX]; @@ -141,6 +156,11 @@ char *str_leet(char *s) { return buf; } +/** + * Capitalize first character in each word + * @param s input string modified + * @return pointer to s + */ char *str_title_case(char *s) { size_t len; size_t i; @@ -156,6 +176,11 @@ char *str_title_case(char *s) { return s; } +/** + * Randomize characters in a string + * @param s input string (modified) + * @return pointer to s + */ char *str_randomize(char *s) { size_t len; char tmp = 0; @@ -169,6 +194,11 @@ char *str_randomize(char *s) { return s; } +/** + * Randomize words in a string + * @param s input string (modified) + * @return pointer to s + */ char *str_randomize_words(char *s) { char old[OUTPUT_SIZE_MAX]; char buf[OUTPUT_SIZE_MAX]; @@ -189,9 +219,14 @@ char *str_randomize_words(char *s) { return s; } +/** + * Reverse all characters in a string + * @param s input string (modified) + * @return pointer to s + */ char *str_reverse(char *s) { size_t dest; - char buf[1024]; + char buf[OUTPUT_SIZE_MAX]; buf[0] = '\0'; dest = 0; @@ -201,4 +236,4 @@ char *str_reverse(char *s) { buf[dest] = '\0'; strcpy(s, buf); return s; -} +}
\ No newline at end of file |