Last active
January 11, 2018 07:41
-
-
Save s22su/50a438ae61009b2c358a62ad8a7553dd to your computer and use it in GitHub Desktop.
Randomize string case in C
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
| #include <stdio.h> | |
| #include <string.h> | |
| #include <ctype.h> | |
| #include <stdlib.h> | |
| char* randomizeCase(char *str) { | |
| int i = 0; | |
| char* new_str; | |
| new_str = ""; | |
| while (str[i]) | |
| { | |
| char current = str[i]; | |
| if(isdigit(current)) { | |
| asprintf(&new_str,"%s%c",new_str, current); | |
| } | |
| else { | |
| int r = rand() % 2; | |
| if(r == 1) { | |
| asprintf(&new_str, "%s%c", new_str, toupper(current)); | |
| } | |
| else { | |
| asprintf(&new_str, "%s%c", new_str, tolower(current)); | |
| } | |
| } | |
| i++; | |
| } | |
| return new_str; | |
| } | |
| int main(void) { | |
| char* c = "123hell0iAMyoLongStringLOLcat23"; | |
| for(int i = 0; i < 5; i++) { | |
| printf("%s", randomizeCase(c)); | |
| printf("\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment