C-如何更改字符

时间:2018-07-07 06:04:42

标签: c

我试图在C中创建一个用户名生成器。基本上,用户键入他/她的名字,然后程序会基于该名字生成一个名字。它会根据大小写,数字和符号而变化。但是我找不到如何更改字符。例如,用户给出“ Noone”,程序应给出“ N0one”,“ N00ne”,“ N00n3”,“ noone”,“ n0one”等。 到目前为止,这是代码:

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();

}

1 个答案:

答案 0 :(得分:4)

C的pseudo-random number generators数量非常有限,但仍可用于您的用例。

仅遍历字符串,并生成随机的01。如果0对当前字符不执行任何操作;如果为1,则检查当前字符是大写还是小写(例如使用isupperislower)并相应地更改字符大小写(使用toupper或{{3} }。

如果您想随机选择其他字母或数字,请使用第一个数字作为选择器,以查看是否应更改字符,并在036之间生成一个 second 数字。 0。如果1更改大小写,如果1011然后更改为相应的数字,或者如果3610然后减去0并且更改为相应的字母。

您可以通过更改第一个数字的范围来轻松更改更改字符的可能性。例如,您可以在30(包括)之间生成一个数字,并在该值等于'o'的情况下修改字符。然后,您有四分之一的机会修改角色,而不是二分之一。

关于获取特定范围内的数字,如果您稍加注意,Internet上就会有很多示例。


如果您想将某些(或全部)字符修改为外观相似的 字符(如'O''0'struct { int original; int leet; } character_change_table[] = { { 'o', '0' }, { 'e', '3' }, // Etc. }; ),类似于“ leet代码”,那么一个可能的解决方案是为您要翻译的所有字符保留一个查找表。然后,如果您决定“更改”角色,则在查找表中为当前角色随机选择一个相似的对象。

此查找表可能是结构数组,例如

from bs4 import BeautifulSoup
import urllib.request

# Initialize a dictionary
d = {}

# Function to extract moonset times
def GetMoonSet():
    global d

    print("Extracting moonset times...")
    # setup the source
    with urllib.request.urlopen("https://www.timeanddate.com/moon/usa/tampa") as url:
        req = url.read()

    soup = BeautifulSoup(req, "html.parser")
    the_rows = soup('table', {'id': "tb-7dmn"})[0].tbody('tr')

    for row in the_rows:
        col_header = row.findChildren("th")[0]
        day = col_header.getText().strip(' \t\n\r')
        d[day] = 'NA'
        cols = row.findChildren("td")
        for col in cols:
            if col.get('title') != None and col.get('title').startswith("The Moon sets in") and col.get('class') != None and len(col.get('class')) == 2:
                d[day] = col.getText()
                continue

# Collect moonset times
GetMoonSet()

# Ask for date from user and print the corresponding moonset time
while True:
    date = input("Please enter a valid date for this month: ")
    if int(date) < 1 or int(date) > 31:
        continue
    else:
        print("Moonset time on {} is {}.".format(date, d[date]))
        break