zoukankan      html  css  js  c++  java
  • C#入门经典第五版之变量的更多内容编码题训练

    1. 编写一个控制台应用程序,它接收用户输入的一个字符串,将其中的字符以与输入相反的顺序输出。

    1         public string ReverseString(string str)
    2         {
    3             string reversedString = "";
    4             for (int i = str.Length - 1; i >= 0; i--)
    5             {
    6                 reversedString += str[i];
    7             }
    8             return reversedString;
    9         }
    View Code

    2. 编写一个控制台应用程序,它接收用户输入的一个字符串,用yes替换字符串中所有的no

    1         public string ReplaceString(string str)
    2         {
    3             str = str.Replace("no", "yes");
    4             return str;
    5         }
    View Code

    3. 编写一个控制台应用程序,它接收用户输入的一个字符串,给字符串的每个单词加上双引号

    1         public string AddQuotes(string str)
    2         {
    3             str = """ + str.Replace(" ", "" "") + """;
    4             return str;
    5         }
    View Code

    首先,将以上三个方法放入Class StringExample中,然后,就可以在Program.cs中,建立一个测试类,通过以下方式调用并输出字符串结果:

     1         private static void StringExampleTest()
     2         {
     3             StringExample ex = new StringExample();
     4             string reversedString = "I am a Student";
     5             string resultStr;
     6             resultStr = ex.ReverseString(reversedString);
     7             Console.WriteLine(resultStr);
     8             string replaceString = "no, I am no ok, there is nobody here";
     9             resultStr = ex.ReplaceString(replaceString);
    10             Console.WriteLine(resultStr);
    11             string AddQuotes = "We are children, we are young";
    12             resultStr = ex.AddQuotes(AddQuotes);
    13             Console.WriteLine(resultStr);
    14         }
    View Code

    在Main方法中直接调用上述StringExampleTest()方法即可得到如下结果:

    作者:Ribbon 出处: http://www.cnblogs.com/Ribbon/ 本文版权归作者和博客园共有,欢迎转载。未经作者同意下,必须在文章页面明显标出原文链接及作者,否则保留追究法律责任的权利。 如果您认为这篇文章还不错或者有所收获,可以点击右下角的【推荐】按钮,因为你的支持是我继续写作,分享的最大动力!
  • 相关阅读:
    运输装备(codevs 1669)
    考前复习(codevs 2837)
    2014编程之美初赛第一场
    51系列小型操作系统精髓 简单实现
    数学----有趣的扑克牌《一》
    hadoop编程:分析CSDN注冊邮箱分布情况
    [动态规划]UVA437
    Swift学习笔记四:数组和字典
    [动态规划]UVA10285
    freemarker中的left_pad和right_pad
  • 原文地址:https://www.cnblogs.com/Ribbon/p/4497915.html
Copyright © 2011-2022 走看看