Skip to content

Instantly share code, notes, and snippets.

@s22su
Last active January 11, 2018 07:41
Show Gist options
  • Select an option

  • Save s22su/50a438ae61009b2c358a62ad8a7553dd to your computer and use it in GitHub Desktop.

Select an option

Save s22su/50a438ae61009b2c358a62ad8a7553dd to your computer and use it in GitHub Desktop.
Randomize string case in C
#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