04. 学习字符串

学习字符串:

 1 /**
 2  *   一: 判断回文字符串。
 3  *   例如: rotor 就是回文字符串。
 4  *  
 5  */
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Linq;
 9 using System.Text;
10 using System.Threading.Tasks;
11 
12 namespace SecondAssignment
13 {
14     class Program
15     {
16         /// <summary>
17         /// 反转字符串
18         /// </summary>
19         /// <param name="strOrignial">原字符串</param>
20         /// <returns>反转之后的字符串</returns>
21         private string RverseString(string strOrignial)
22         {
23             string strResult = null;
24             for (int i = strOrignial.Length - 1; i >= 0; i--)
25             {
26                 string strTemp = strOrignial.Substring(i, 1);
27                 strResult = strResult + strTemp;//字符串累加
28             }
29             return strResult;
30         }
31         //判断是否是回文字符串(如:rotor)
32         private void Text()
33         {
34             string strOrstr = "rotor";
35             string strRverseResult = RverseString(strOrstr);
36             if (strRverseResult == strOrstr)
37             {
38                 Console.WriteLine("是回文字符串");
39             }
40             else
41             {
42                 Console.WriteLine("不是回文字符串");
43             }
44         }
45         static void Main(string[] args)
46         {
47             Program p = new Program();
48             p.Text();
49             Console.ReadKey();
50         }
51     }
52 }

 

 1 /**
 2  *   二:请找出1-256中所有的回文数。
 3  *  
 4  */
 5 using System;
 6 using System.Collections.Generic;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 
11 namespace SecondAssignment
12 {
13     class Program
14     {
15         /// <summary>
16         /// 反转字符串
17         /// </summary>
18         /// <param name="strOrignial">原字符串</param>
19         /// <returns>反转之后的字符串</returns>
20         private string RverseString(string strOrignial)
21         {
22             string strResult = null;
23             for (int i = strOrignial.Length - 1; i >= 0; i--)
24             {
25                 string strTemp = strOrignial.Substring(i, 1);
26                 strResult = strResult + strTemp;//字符串累加
27             }
28             return strResult;
29         }
30         //判断是否是回文字符串(如:rotor)
31         private bool Text(string strOrstr)
32         {
33             string strRverseResult = RverseString(strOrstr);
34             if (strRverseResult == strOrstr)
35             {
36                 return true;
37             }
38             else
39             {
40                 return false;
41             }
42         }
43         //输出1-256的所有回文字符串
44         private void Text1()
45         {
46             for (int i = 1; i < 257; i++)
47             {
48                 //判断i是否为回文字符串
49                 bool bb = Text(i.ToString());
50                 if (bb)
51                 {
52                     Console.WriteLine(i);
53                 }
54             }
55         }
56         static void Main(string[] args)
57         {
58             Program p = new Program();
59             p.Text1();
60             Console.ReadKey();
61         }
62     }
63 }

 

 1 /**
 2  *    三: 写一个方法,对给定的 Emall 字符串做检验。
 3  *    要求: 1)要求字符串中包含“@”字符与“.”字符。
 4  *           2)要求字符串中@ 字符在 . 字符之前。
 5  */
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Linq;
 9 using System.Text;
10 using System.Threading.Tasks;
11 
12 namespace SecondAssignment
13 {
14     class Program
15     {
16         //判断 Emall 是否合法
17         public void JudgeEmall()
18         {
19             Console.WriteLine("请输入一个Emall,系统将判断他是否合法:");
20             string _Emall = Console.ReadLine();
21             if (_Emall.IndexOf("@") < _Emall.IndexOf(".") && _Emall.IndexOf("@") >= 0)
22             {
23                 Console.WriteLine("该 Emall 合法!");
24             }
25             else
26             {
27                 Console.WriteLine("该 Emall 不合法!");
28             }
29         }
30         static void Main(string[] args)
31         {
32             Program p = new Program();
33             p.JudgeEmall();
34             Console.ReadKey();
35         }
36     }
37 }

 

 1 /**
 2  * 
 3  *     四: 统计文本行中单词的个数。
 4  *     例如: 输入“I am a student”
 5  *     则程序的运行结果输出为 : 4个单词。
 6  */
 7 using System;
 8 using System.Collections.Generic;
 9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 
13 namespace SecondAssignment
14 {
15     class Program
16     {
17         //统计文本中单词的个数
18         public void Calculate()
19         {
20             Console.WriteLine("请输入一个文本,系统将统计文本中单词的个数:");
21             string _myStr = Console.ReadLine();
22             string[] strArray = null;
23             strArray = _myStr.Split(' ');
24             Console.WriteLine("文本中单词的个数为:{0} 个单词",strArray.Length);
25         }
26         static void Main(string[] args)
27         {
28             Program p = new Program();
29             p.Calculate();
30             Console.ReadKey();
31         }
32     }
33 }

 

 1 /**
 2  *     五: 日期的常用格式具有如下两种:
 3  *     2003-11-29 和 十一 29,2003
 4  *     从键盘读入几行格式的日期,编程输出第二种格式的日期。
 5  */
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Globalization;
 9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 
13 namespace SecondAssignment
14 {
15     class Program
16     {
17         //从键盘读入几行格式的日期,编程输出第二种格式的日期。
18         public void ConvertFormat()
19         {
20             Console.WriteLine("请输入一行日期(如:2017-10-6),系统将转换为第二种格式:");
21             string _DateStr = Console.ReadLine();
22             DateTime time = Convert.ToDateTime(_DateStr);   //将输入的字符串转换为日期
23             int _month = time.Month;                        //提取出输入的月份给 ConvertChinese() 方法调用
24 
25             Console.WriteLine("第二种格式日期为:{0} {1},{2}", ConvertChinese(_month), time.Day, time.Year);
26         }
27         //将数字转换为中文
28         public string ConvertChinese(int month)
29         {
30             string monthChinese = null;
31             switch (month)
32             {
33                 case 1:
34                     monthChinese = "";
35                     break;
36                 case 2:
37                     monthChinese = "";
38                     break;
39                 case 3:
40                     monthChinese = "";
41                     break;
42                 case 4:
43                     monthChinese = "";
44                     break;
45                 case 5:
46                     monthChinese = "";
47                     break;
48                 case 6:
49                     monthChinese = "";
50                     break;
51                 case 7:
52                     monthChinese = "";
53                     break;
54                 case 8:
55                     monthChinese = "";
56                     break;
57                 case 9:
58                     monthChinese = "";
59                     break;
60                 case 10:
61                     monthChinese = "";
62                     break;
63                 case 11:
64                     monthChinese = "十一";
65                     break;
66                 case 12:
67                     monthChinese = "十二";
68                     break;
69                 default:
70                     break;
71             }
72             return monthChinese;
73         }
74         static void Main(string[] args)
75         {
76             Program p = new Program();
77             p.ConvertFormat();
78             Console.ReadKey();
79         }
80     }
81 }

 

posted @ 2017-10-08 23:06  LiuChangwei  阅读(303)  评论(0编辑  收藏  举报