语言C中的随机字符串 [英] Random string in language C

查看:116
本文介绍了语言C中的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要制作一个在C中生成随机字符串的程序,但我不知道该怎么做。

有人能帮帮我吗?

对不起我的英语不好。

坦克。

Hi,
I need to make a program that generates a random strings in C, but i don't know how to do it.
Could anyone help me?
Sorry my english bad.
Tanks.

推荐答案

这可能对你有帮助....



This may help you....

void gen_random(char *s, const int len) {
    static const char alphanum[] =     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 0;
}


我认为GUID和Regex可以让这更容易和可靠...



请尝试以下代码,每次创建一个20长度的随机不同字符串。







I think GUID and Regex could make this much easier and reliable...

Do try the following code which creates a random different string of 20 length each time.



Guid guid = Guid.NewGuid();

           string id = guid.ToString().Substring(0, 20);
           Regex R1 = new Regex(id);
           id = Regex.Replace(id, @"\s|\-|'", "");

           Response.Write(id);







希望这将有助于.... :)




Hope that will help to.... :)


这篇关于语言C中的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆