-1

I am trying to make a username generator in C. Basically user types his/her name and program generates a name based on it. It's changing by case, numbers and symbols. But I couldn't find how to change characters. Like, user gives "Noone", program should give "N0one","N00ne","N00n3","noone","n0one" etc. This is the code so far:

int main() {
    char name[150];
    char confirm[150];
    char tru[15]="y";
    char fals[15]="n";
    SetConsoleTitle("Username Generator");
    printf("Welcome to the username generator!\n");
    while (1==1) {
        printf("Type your name: ");
        fgets(name,15,stdin);
        printf("\nYour name is ");
        puts(name);

        printf("\nDo you confirm? ");
        fgets(confirm,15,stdin);

        if (strcmp(confirm,fals) == 0) {
            continue;
        }
        if (strcmp(confirm,tru) == 0) {
            printf(" \n Ok.");
            break;
        }
        else {
            printf("\ninvalid statement\n");
        }
    }
    printf("Do you want case randomise?\n");
    char csens[15];
    fgets(csens,15,stdin);
    if (strcmp(csens,tru) == 0){
        printf("\nOk");  
    }
    if (strcmp(csens,fals) == 0){
        printf("\nOk");  
    }
    printf("\nStarting to generate");
              //missing part
    printf("\nPress any button to exit");
    getchar();

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Don't use `gets`. Never ***ever*** use `gets`. It's a dangerous function which have been obsolete for a long time, and even removed from C in the last C specification seven years ago. Learn how to use e.g. [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Jul 07 '18 at 06:08
  • Despite taking about 20 lines of code to enter a name, with comparisons and queries, you never once check anything important. – Weather Vane Jul 07 '18 at 06:09
  • 3
    There's a question about [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) which outlines alternatives too. – Jonathan Leffler Jul 07 '18 at 06:34
  • If you drop 'randomly' from the title, you need to drop it from the body of the question too, but you shouldn't be dropping it because you've already got an answer which addresses your original question (the one mentioning 'randomly'). – Jonathan Leffler Jul 07 '18 at 06:37
  • Yes i changed, it i didnt want it randomly, i didnt even see while typing. mb – murdofagnu Jul 07 '18 at 06:38
  • Typing 'randomly' when you didn't mean to is a fairly big mistyping — especially as you did it twice. What does that mean? You want all possible leet-speak spellings? Methinks probably not — but maybe you do. If you do need them all, you have to work moderately hard. It's a lot easier to produce one such name, and then if that isn't OK, offer a second — using random choices to determine the transliteration. – Jonathan Leffler Jul 07 '18 at 06:42
  • Only you.... *transliteration* -- you must keep a list somewhere `:)` A strong second to a procedural as opposed to a random approach. A logical start with a complete conversion to lowercase, check whether the name is available, and then proceed character-by-character converting to uppercase, and then proceed from there (maybe a toggle of all even indexed characters to lower). Randomly setting up users (or doing anything to administer a system aside from generating passwords -- seems like a bad idea...) – David C. Rankin Jul 07 '18 at 07:05
  • Actually yes, I want to produce all leet names, my friend asked me to this and i throught I could learn somethings so i said why not – murdofagnu Jul 07 '18 at 07:17

1 Answers1

4

C have a very limited amount of pseudo-random number generators, but it's still usable for your use-case.

Simply loop over the string, and generate a random 0 or 1. If 0 do nothing to the current character; If 1 then check if the current character is upper or lower case (using e.g. isupper or islower) and change the character case accordingly (using toupper or tolower).

If you want to random select another letter or digit, then use the first number as a selector to see if the character should be changed, and generate a second number between 0 and 36. If 0 then change case, if 1 to 10 then change into the corresponding digit, or if 11 to 36 then subtract 10 and change to the corresponding letter.

You can easily change the probabilities of the chances of changing a character by changing the range of the first number. For example you can generate a number between 0 and 3 (inclusive) and modify the character if the value is equal to 0. Then you have a one-in-four chance of modifying a character, instead of a one-in-two.

As for getting a number in a specific range, there are a lot of examples all over the Internet, if you serch a little.


If you want to modify some (or all) characters to similar looking characters (like 'o' or 'O' to '0'), similar to "leet code", then a possible solution is to keep a lookup-table for all characters which you want to possibly translate. Then if you decide to "change" the character then randomly select a look-alike in the lookup-table for the current character.

This lookup-table could be an array of structures, like

struct
{
    int original;
    int leet;
} character_change_table[] = {
    { 'o', '0' },
    { 'e', '3' },
    // Etc.
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621