C++:随机生成10~100字符的大小写英文字母字符串,作下列要求的操作,咋写?

要求:
1.输入Y或y程序运行一次,无限循环;输入其他字符则退出程序。
2.先原串输出一次,再大写字母在前小写字母在后输出一次。
3.然后逆序小写字母在前、大写字母在后输出一次。
4.最后逆序输出原串。

代码文本:

//#include "stdafx.h"//vc++ 6.0? Maybe should add this line.

#include <stdlib.h>

#include <string>

#include <iostream>

#include "math.h"

#include "time.h"

using namespace std;

int main(int argc,char *argv[]){

string s;

char i,k,t;

srand((unsigned)time(NULL));//随机种子

while(cout << "Y/N==",(cin >> k) && (k=='Y' || k=='y')){

for(s="",i=rand()%91+10;i--;)//生成10~100大小写字母串

s+=rand()%26 + (rand()%2 ? 65 : 97);

cout << s << endl;//原串输出

for(t=s.length(),i=0;i<t;i++)//先输出大写

if(s[i]>='A' && s[i]<='Z')

cout << s[i];

for(i=0;i<t;i++)//接上小写输出

if(s[i]>='a' && s[i]<='z')

cout << s[i];

cout << endl;

for(i=t-1;i>=0;i--)//逆序先输出小写

if(s[i]>='a' && s[i]<='z')

cout << s[i];

for(i=t-1;i>=0;i--)//逆序接上输出小写

if(s[i]>='A' && s[i]<='Z')

cout << s[i];

cout << endl;

for(t--;t>=0;cout << s[t--]);//逆序输出原串

cout << "\n\n";

}

return 0; 

(供参考)

温馨提示:答案为网友推荐,仅供参考
相似回答