字符串习题

2017-08-08  本文已影响0人  妈妈说喝牛奶能长个

1、在控制台输入2个单词,hello和world,然后组成一句话并输出。

string str = Console.ReadLine();

string str1 = Console.ReadLine();

string str2 = str +" " + str1;

Console.WriteLine ("{0}",str2);

2、输入班上所有同学的名字,输入完成后输出所有姓刘的学生的名字。

string names = "";

for (int i = 0; i < 5; i++) {

                    string str = Console.ReadLine ();

if (str.StartsWith("刘")) {

                    names +=( str+" ,");

           }

}

Console.WriteLine ("班上所有姓刘的人为:{0}",names);


3、输入一个字符,判断它如果是小写字母输出其对应大写字母,如果是大写字母输出其对应小写字母,

如果是数字输出其相反数,如果是空格,输出“space”,

如果不是上述情况,输出“other”。(提示使用Console.Read()

int n = Console.Read();                       //ACLL码

if (n >= 65 && n <= 90) {                    //大写转小写

                Console.WriteLine ("{0}", (char)(n + 32));    //int值强转字符

} else if (n >= 97 && n <= 122) {       //小写转大写

                Console.WriteLine ("{0}", (char)(n - 32));

} else if (n >= 48 && n <= 57) {

                Console.WriteLine ("{0}",-(n - 48));

} else if (n == 32) {

                Console.WriteLine ("Space");

} else {

                Console.WriteLine ("Other");

}

4、已知abc + cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有的组合;

//987                                     (int)(987/100) = 9       

//987  987%100 = 87         (int)(87/10)=8

//987                                      987%10 = 7

for (int a = 1; a < 10; ++a) {

        for (int b = 0; b < 10; ++b) {

               for (int c = 1; c < 10; ++c) {

                        if ((a*100+b*10+c)+(c*100+b*10+a) == 1333) {

                        Console.WriteLine ("a={0},b={1},c={2}",a,b,c);

                                              }

                            }

               }

}

上一篇 下一篇

猜你喜欢

热点阅读