国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中倒序輸出字符串的方法示例

2019-10-29 21:16:04
字體:
供稿:網(wǎng)友

前言

本文將演示如何將字符串的單詞倒序輸出。注意:在這里我不是要將“John” 這樣的字符串倒序為成“nhoJ”。這是不一樣的,因為它完全倒序了整個字符串。而以下代碼將教你如何將“你 好 我是 緹娜”倒序輸出為“緹娜 是 我 好 你”。所以,字符串的最后一個詞成了第一個詞,而第一個詞成了最后一個詞。當然你也可以說,以下代碼是從最后一個到第一個段落字符串的讀取。

對此我使用了兩種方法。

第一種方法僅僅采用拆分功能。

根據(jù)空格拆分字符串,然后將拆分結(jié)果存放在一個string類型的數(shù)組里面,將數(shù)組倒序后再根據(jù)索引打印該數(shù)組。

代碼如下

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 將字符串的單詞倒序輸出{ class Program {  static void Main(string[] args)  {   Console.ForegroundColor = ConsoleColor.White;   Console.WriteLine("請輸入字符串:");   Console.ForegroundColor = ConsoleColor.Yellow;   string s = Console.ReadLine();   string[] a = s.Split(' ');   Array.Reverse(a);   Console.ForegroundColor = ConsoleColor.Red;   Console.WriteLine("倒序輸出結(jié)果為:");   for (int i = 0; i <= a.Length - 1; i++)   {    Console.ForegroundColor = ConsoleColor.White;    Console.Write(a[i] + "" + ' ');   }   Console.ReadKey();  } }}

輸出結(jié)果

c,字符串倒序輸出,C#倒序輸出字符串

第二種方法

我不再使用數(shù)組的倒序功能。我只根據(jù)空格拆分字符串后存放到一個數(shù)組中,然后從最后一個索引到初始索引打印該數(shù)組。

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 將字符串的單詞倒序輸出{ class Program {  static void Main(string[] args)  {   Console.ForegroundColor = ConsoleColor.White;   Console.WriteLine("請輸入字符串:");   Console.ForegroundColor = ConsoleColor.Yellow;   int temp;   string s = Console.ReadLine();   string[] a = s.Split(' ');   int k = a.Length - 1;   temp = k;   for (int i = k; temp >= 0; k--)   {    Console.Write(a[temp] + "" + ' ');    --temp;   }   Console.ReadKey();  } }}

輸出結(jié)果

c,字符串倒序輸出,C#倒序輸出字符串

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用C#能帶來一定的幫助,如果有疑問大家可以留言交流。


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 山丹县| 奉新县| 新疆| 海盐县| 邹城市| 饶阳县| 临桂县| 柞水县| 灵宝市| 曲阳县| 怀宁县| 元谋县| 当雄县| 长海县| 扬中市| 牙克石市| 张家口市| 宜川县| 循化| 招远市| 杭锦后旗| 北安市| 凤凰县| 綦江县| 句容市| 织金县| 永修县| 武鸣县| 鄂尔多斯市| 安康市| 咸丰县| 琼结县| 四川省| 五大连池市| 濮阳市| 当涂县| 焦作市| 株洲县| 射洪县| 丰县| 海南省|