- 1 Post
- 13 Comments
maybe void? its rolling relase, stable and boring in a good way
hkwln@lemmy.mlto
C Programming Language@programming.dev•[Beginner] Call all functions from main() or jump to functions from within functions?English
2·4 months agoNo problem, If you have any further questions about my or your code, feel free to ask.
That’s so kind of you😊, please don’t feel like you have to, was a pleasure to help, my ko-fi: https://ko-fi.com/hkwln
hkwln@lemmy.mlto
C Programming Language@programming.dev•[Beginner] Call all functions from main() or jump to functions from within functions?English
2·5 months agoI think this is more about readability and style. I have looked at your codei and changed a few things😅, maybe this helps you. This is how I would do it: (I am as well far from expert level in C)
CODE
#include <limits.h> // added for INT_MIN #include <stdio.h> #include <stdlib.h> // added for exit() #include <string.h> // you use scanf getchar and fgets, I would choose one // Function declarations, also chooose your naming convention! void prompt_choice_main(char *choice); void prompt_choice_main_again(void); void prompt_input(void); int prompt_choice_trim(void); void (*execute_choice[])(void); void trim_numbers(void); void trim_whitespace(void); void trim_letters(void); void trim_special(void); void specify_special(void); void print_result(char input[]); // Global variables; I would put as much of these as parameters/structs or // return values because of clearer ownership and readability char choice_detail = 0x00; char choice_special = 0x00; char input[1000] = ""; char previous_input[1000] = ""; // Remove specific numbers, letters, punctuation or whitespace characters from // input. int main() { printf( "\nWelcome! This program trims text by removing unwanted characters.\n"); while (1) { char choice_main = 0x00; prompt_choice_main(&choice_main); if (choice_main == 'T') { prompt_input(); int choice_trim = prompt_choice_trim(); execute_choice[choice_trim](); print_result(input); } else if (choice_main == 'P') { snprintf(input, 1000, "%s", previous_input); // i would prefer snprintf instead of sprintf // because of safety printf("\nYou are trimming previously trimmed text: %s\n", input); int choice_trim = prompt_choice_trim(); execute_choice[choice_trim](); print_result(input); } else if (choice_main == 'E') { printf("\nGoodbye!\n"); exit(0); } } return 0; } // Function definitions void prompt_choice_main(char *choice_main) { while (1) { printf("\nPress T and ENTER to trim text"); if (strlen(previous_input) != 0) { printf(", P for previously trimmed text "); } printf(" or E and ENTER to exit: "); *choice_main = getchar(); while (getchar() != '\n') { } if (*choice_main == 'T' || *choice_main == 'E' || *choice_main == 'P') { break; } else { printf("\nInvalid input!\n"); } } } void prompt_input(void) { printf("\nEnter the text that you would like to trim and press ENTER: "); size_t c = 0; do input[c++] = getchar(); while (input[c] != '\n'); input[c] = '\0'; } int prompt_choice_trim(void) { while (1) { int choice_trim = 0; printf("\nWhat would you like to trim?\n1) Numbers (1, 2, 3...)\n2) " "Whitespace (space, tab or newline) \n3) Letters (A,B,C... " "a,b,c...)\n4) Special characters (!,?, . , ...)\nType one of the " "above numbers and press ENTER: "); char c; c = getchar(); choice_trim = c - '0'; while (getchar() != '\n') { } if (choice_trim < 1 || choice_trim > 4) { printf("\nInvalid choice!\n"); } else { return choice_trim - 1; } } // INFO: Through the return type you can do a bit of error handling i you did // not already now return INT_MIN; } // Here I would use a function pointer, but your version is also perfectly fine void (*execute_choice[])(void) = { trim_numbers, // 123 etc trim_whitespace, // space, tab, newline trim_letters, // ABC..., abc... trim_special, // ! ? , . etc. }; // I would not replace characters with 0x18 but instead keep chars which a valid // and delete chars which are unvalid in a void trim_numbers(void) { size_t w = 0; for (size_t r = 0; input[r] != '\0'; r++) { if (input[r] != '0' && input[r] != '1' && input[r] != '2' && input[r] != '3' && input[r] != '4' && input[r] != '5' && input[r] != '6' && input[r] != '7' && input[r] != '8' && input[r] != '9') { input[w++] = input[r]; } } input[w] = '\0'; return; } void trim_whitespace(void) { while (1) { printf("\nType S to trim SPACE, T to trim TAB, N to trim NEWLINE or A to " "trim all whitespace: "); scanf("%c", &choice_detail); while (getchar() != '\n') { } if (choice_detail == 'S' || choice_detail == 'T' || choice_detail == 'A') { break; } else { printf("\nInvalid input!\n"); } } int n = 0; for (n = strlen(input) - 1; n >= 0; n--) { if (choice_detail == 'S') { if (input[n] == 0x20) { input[n] = 0x18; } } // space else if (choice_detail == 'T') { if (input[n] == 0x09) { input[n] = 0x18; } } // tab else if (choice_detail == 'N') { if (input[n] == 0x0A) { input[n] = 0x18; } } // newline else if (choice_detail == 'A') { if (input[n] == 0x20 || input[n] == 0x09 || input[n] == 0x0A) { input[n] = 0x18; } } } return; } void trim_letters(void) { while (1) { printf("\nType U to trim uppercase letters, L to trim lowercase letters " "or A to trim all letters: "); scanf("%c", &choice_detail); while (getchar() != '\n') { } if (choice_detail == 'U' || choice_detail == 'L' || choice_detail == 'A') { break; } else { printf("\nInvalid input!\n"); } } int n = 0; for (n = strlen(input) - 1; n >= 0; n--) { if (choice_detail == 'U') { if (input[n] >= 0x41 && input[n] <= 0x5A) { input[n] = 0x18; } } // Uppercase else if (choice_detail == 'L') { if (input[n] >= 0x61 && input[n] <= 0x7A) { input[n] = 0x18; } } // Lowercase else if (choice_detail == 'A') { if (input[n] >= 0x41 && input[n] <= 0x5A || input[n] >= 0x61 && input[n] <= 0x7A) { input[n] = 0x18; } } } return; } void trim_special(void) { while (1) { printf("\nType A to trim all special characters or S to specify which " "character to remove: "); scanf("%c", &choice_detail); while (getchar() != '\n') { } if (choice_detail == 'A' || choice_detail == 'S') { break; } else { printf("\nInvalid input!\n"); } } int n = 0; for (n = strlen(input) - 1; n >= 0; n--) { if (choice_detail == 'A') { if (input[n] >= 0x21 && input[n] <= 0x2F || input[n] >= 0x3A && input[n] <= 0x40 || input[n] >= 0x5B && input[n] <= 0x60 || input[n] >= 0x7B && input[n] <= 0x7E) { input[n] = 0x18; } } // All whitespace } if (choice_detail == 'S') { specify_special(); } // Let user specify character. return; } void specify_special(void) { while (1) { printf("\nEnter special character to trim and press ENTER: "); scanf("%c", &choice_special); while (getchar() != '\n') { } if (choice_special >= 0x21 && choice_special <= 0x2F || choice_special >= 0x3A && choice_special <= 0x40 || choice_special >= 0x5B && choice_special <= 0x60 || choice_special >= 0x7B && choice_special <= 0x7E) { break; } else { printf("\nNot a special character!\n"); } } int n = 0; for (n = strlen(input) - 1; n >= 0; n--) { if (input[n] == choice_special) { input[n] = 0x18; } } return; } void print_result(char input[]) { printf("\nTrimmed text:\n\n%s\n", input); sprintf(previous_input, "%s", input); // Save trimmed text for reuse. return; } // TODO // Create error handling when trimming non existing characters. // Replace characters (uppercase/lowercase, user selected, etc).
hkwln@lemmy.mlto
C Programming Language@programming.dev•Is this a limitation of scanf() or bad control flow?
3·5 months agoyou could also use getchar() to only take the first char out of the buffer, if you didn’t know already ;)
hkwln@lemmy.mlto
Linux@lemmy.ml•Is anybody running Linux on ARM laptops? (Mediatek, Qualcomm Snapdragon, etc.)
1·7 months agomy mate runs ubuntu on snapdragon elite
when you use the terminal, why use vlc when you can use ffmpeg?
hkwln@lemmy.mlto
Technology@lemmy.world•'What a great way to kill your community': Discord users are furious about its new age verification checks — and are now hunting for alternativesEnglish
1·9 months agothe single problem is when you never codet in rust:(. I am only able to code in C and python an a bit of java
hkwln@lemmy.mlto
Technology@lemmy.world•'What a great way to kill your community': Discord users are furious about its new age verification checks — and are now hunting for alternativesEnglish
3·9 months agodid you tried it, what is the experience like?
hkwln@lemmy.mlto
Games@lemmy.world•Discord will restrict your account next month unless you scan ID or faceEnglish
5·9 months agoDo you have any suggestions for a better/open-source alternative?
when i was a beginner, it helped me a lot to just start, i installled linux mint without any knowledge. Doing easy things( system update, installing things, using nano, git, pip) Then i got used to do things more and more in the terminal. I had two laptops, one hacket chromebook with linux and one with windows, my safe space. Now my safe space is arch😅
hkwln@lemmy.mlto
Asklemmy@lemmy.ml•What is something you've done that you believe very few other people have done? Only one caveat: it can't be cool or jealousy-provoking.Deutsch
2·2 years agoI was bringin sandals with me and hike-off with them
hkwln@lemmy.mlto
Asklemmy@lemmy.ml•What is something you've done that you believe very few other people have done? Only one caveat: it can't be cool or jealousy-provoking.Deutsch
5·2 years agoI once hiked onto a mountain barefoot because of a bet with a friend, it took me 3 1/2 hours untl I have reached the summit.

hey, I started going into kernel dev recently and found it real interresting, even made my own miniature kernel in C and arm :).Is there a possibility to contribute something small to your kernel? would love to learn more things about that.