C#字符串的倒序输出

介绍

在本文中,我将演示如何将字符串的单词倒序输出。在这里我不是要将“John”
这样的字符串倒序为成“nhoJ”,。这是不一样的,因为它完全倒序了整个字符串。而以下代码将教你如何将“你 好 我是 缇娜”倒序输出为“缇娜 是 我 好 你”。所以,字符串的最后一个词成了第一个词,而第一个词成了最后一个词。当然你也可以说,以下代码是从最后一个到第一个段落字符串的读取。

对此我使用了两种方法。第一种方法仅仅采用拆分功能。根据空格拆分字符串,然后将拆分结果存放在一个string类型的数组里面,将数组倒序后再根据索引打印该数组。

代码如下

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("倒序输出结果为:");
            for (int i = 0; i <= a.Length - 1; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(a[i] + "" + ‘ ‘);
            }
            Console.ReadKey();
        }
    }
}

输出结果

对于第二种方法,我不再使用数组的倒序功能。我只根据空格拆分字符串后存放到一个数组中,然后从最后一个索引到初始索引打印该数组。

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();
        }
    }
}

输出结果

时间: 2024-08-26 23:16:11

C#字符串的倒序输出的相关文章

字符串的倒序输出

package cn.itsource.homeworkday14; /** *  字符串的倒序输出: * 把字符串翻转过来输出 原字符串“avdkfasjks”输出效果”skjsafkdva”; */ //1.通过创建StringBuffer调用它的reverse()方法字符串倒序输出(比较简洁) public class Seven {public static void main(String[] args) { String str6 = "abcdefgh";    Stri

JAVA面试题之实现字符串的倒序输出

1 package shb.java.demo; 2 3 public class MyTest { 4 public static void main(String[] args) { 5 String string = "For God so loved the world kdkdk"; 6 reverse_2(string); 7 } 8 //方法一 9 public static void reverse(String str){ 10 11 String[] strings

Java实现给定字符串的倒序输出

1.除2判中法: public String orderDesc(String str){ byte [] bytes = str.getBytes(); for ( int i = 0; i < bytes.length / 2 ; i++) { Byte b = bytes [i] ; bytes [i] = bytes [bytes.length - 1 -i ] ; bytes [bytes.length - 1 -i ] = b ; } return new String (bytes

C语言之基本算法40—字符串删除元音字母倒序输出

//字符串,数组 /* ================================================================== 题目: 输入一行字符,将辅音字母按反序输出(去掉元音字母),并存放在另一字符串! ================================================================== */ #include<stdio.h> #include<string.h> #define N 256 vo

Java实现字符串倒序输出的几种方法

1. 最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了. import javax.swing.JOptionPane; public class ReverseString { public static void main (String args[]){ String originalString; String resultString = ""; originalString = JOptionPane.showInputDialog

Java基础知识强化08:将字符串倒序输出(包括空格)的几种方法

1.最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了: 1 package himi.hebao05; 2 3 public class TestDemo02 { 4 public static void main(String[] args) { 5 int i = 0; 6 String text = "hebao I love you!"; 7 String result = " "; 8 char[] charArr

java习题:倒序输出一行字符串

倒序输出一行字符串: public static void main(String[] args) { System.out.println("请输入一行字符串(按Enter执行):"); Scanner input = new Scanner(System.in); String str = input.next(); daoxu(str); } /** * 将字符串倒序 * @param str */ public static void daoxu(String str) { S

php中怎样自己定义,倒序输出一个字符串

利用strrev--这个函数 strrev — 反转字符串 $str="ABCDEFGHIJK"; $new_str=strrev($str); echo $new_str; //输出结果KJIHGFEDCBA: 第二种方法,自定义 $str="qwertyuiop"; my_rev($str); function my_rev($str) { $len = strlen($str);//先计算长度 $new_str = "";//定义一个空的字

【python】 倒序输出字符串

By Dolphin , 20150730 Title : 编写一个程序,获取用户输入的一条信息,然后将其倒序输出. Code: # Reverse Word # By Dolphin,20150730 # word = input("Please Enter a word :") # creat a jumble word  jumble = "" position = len(word) - 1 while word and position != -1 : j