字符串反转····



 

一、系统自带的字符串反转的方法:

  static void Main(string[] args)        

{              

      String str =Console.ReadLine();          

      char[] arry = str.ToCharArray();

              Array.Reverse(arry);       

      Console.WriteLine(arry);   

      Console.ReadKey();

 


 

二、下面我们就用把字符串拆分成字符数组,然后用遍历,来对数组进行反转的实现。

 1 namespace ConsoleApplication1  2 {  3     class Program  4  {  5         static void Main(string[] args)  6  {  7             String str =Console.ReadLine();  8             char[] oldchar = str.ToCharArray();//原来的字符串转换成字符数组
 9             
10             int l = str.Length;//计算长度,为循环做准备。
11             char[] newchar=new char[l];//创建一个相同长度的数组用来存储翻转后的新数组。
12 
13             for (int i = 0; i < l; i++) 14  { 15                 newchar[i] = oldchar[l - i - 1];//将oldchar[]数组的最后一个给newchar[]数组的第一个,这样就反转了
16              
17 
18  } 19             Console.Write(new string(newchar));//重新调用构造函数进行实例化一个新的反转后的字符串。
20  Console.ReadKey(); 21            
22 
23            
24             
25            
26  } 27         
28 
29         
30     }

三、1/2

方法三其实就是方法二的一个延伸,比如字符串:a b c d ,我们可以首尾依次对调,d b c a(一次),d c b a(两次)。所以可以看出,其实可以循环次数知识长度的一半就可以了!!其中首位依次对调,这个就是我们平常常见的两个值怎样换值的问题,找个中间人呗。

 1   static void Main(string[] args)  2  {  3             String str =Console.ReadLine();  4             char[] oldchar = str.ToCharArray();//原来的字符串转换成字符数组
 5             
 6             int l = str.Length;//计算长度,为循环做准备。
 7           
 8 
 9             for (int i = 0; i < l/2; i++) 10  { 11                 char middle;//创建个中间人
12                 middle=oldchar[i];//前端值给中间人
13                 oldchar[i] = oldchar[l - i - 1];//对应的后端值,覆盖前端值
14                 oldchar[l - i - 1] = middle;//中间人,把前端值,给对应的后端值
15  
16 
17              
18 
19  } 20             Console.Write(new string(oldchar));//数组重组为一个字符串。
21  Console.ReadKey(); 22         }

 


四、运用StringBuilder,提供的方法较为简单.

 1  class Program  2  {  3         static void Main(string[] args)  4  {  5 
 6             string old = Console.ReadLine();  7             StringBuilder stb = new StringBuilder(old.Length);  8           
 9             for (int i=old.Length ; i>0; i--) 10  { 11                 stb.Append(old[i-1]); 12 
13              
14 
15  } 16             Console.Write(stb.ToString());//数组重组为一个字符串。
17  Console.ReadKey(); 18           
19         }

 五、实用栈的方法。栈是根据先进后出的方式处理数据。可以用栈些计算器这样的小程序,下面我们就用栈来进行字符串的反转。

 

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5            Stack<char> sta=new Stack<char>();//定义数组来存储拆分的字符串
 6            string str=Console.ReadLine();
 7            int l = str.Length;
 8            for (int i = 0; i < l; i++)
 9            {
10               sta.Push(str[i]);//压入栈中
11            
12            }
13            char[] c = new char[l];
14            for (int i = 0; i < l; i++)
15            {
16               
17                c[i] = sta.Pop();//出栈
18            }
19            Console.WriteLine(new string(c));
20            Console.ReadLine();
21            
22         }
23 
24 
25 
26     }

这个的思想很重要,虽然用来两次循环,上面那次循环完全可以用foreach遍历。栈是个线性数据结构,有后进后进先出的特点。先进的压入栈底,后进的压入栈顶,出栈时候就从栈顶开始移出。

 


好吧···其实还有很多方法的···就写到这···希望大家多多指正··· 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM