admin管理员组

文章数量:1636930

1. implement a function of String randomAlphabetic(int count)

Creats a random string whose length is the number of characters specified;

Characters will be chosen from the set of alphabetic characters.


#include <iostream>
#include <string>
#include <time.h>
using namespace std;


string randomAlphabetic(int count)
{
string str;
srand((unsigned int)time(NULL));
int random;
char randomChar;


while (count--)
{
random = rand() % 26;
if (rand() % 2 == 0)
{
randomChar = 'a' + random;
}
else
{
randomChar = 'A' + random;
}


str += randomChar;
}


return str;
}


int main()
{
int count;
while (cin>>count)
{
string str = randomAlphabetic(count);
cout<<str<<endl;
}


return 0;
}

本文标签: StringfunctionimplementcountINT