数据结构 c# 字符串的处理

来源:互联网 发布:网吧服务器攻击软件 编辑:程序博客网 时间:2024/04/29 19:55

1. 字符串的逆转

using System;

class Output
{
    public static void Main(string[] args)
    {
        //*********************String reverse******************
        Console.WriteLine("Please input a sentence:");
        string str = Console.ReadLine();
        char[] arrychar = str.ToCharArray();
        for (int i = 0; i < arrychar.Length/2; i++)
        {
            char tem;
            tem = arrychar[i];
            arrychar[i] = arrychar[arrychar.Length - i - 1];
            arrychar[arrychar.Length - i - 1] = tem;
        }
        string arry = new string(arrychar);
        Console.Write(arry);
        Console.ReadKey();
    }
}

2. 输入一字符串要求输出后只有数字和字母,其它的空格特殊字符都不要

using System;

class Output
{
    public static void Main(string[] args)
    {
        //*********************Get Number and Char******************
        Console.WriteLine("Please input a sentence:");
        string str = Console.ReadLine();
        char[] arrychar = str.ToCharArray();
        char[] temp = new char[arrychar.Length];
        int j = 0;
        foreach (char ch in arrychar)
        {          
            if (char.IsLetterOrDigit(ch))
            {
                temp[j] = ch;
                j++;
            }
        }
        string arry = new string(temp);
        Console.Write(arry);
        Console.ReadKey();     

    }
}

 

3. 二分查找算法实现

using System;

namespace Search
{
    public class BinarySearch
    {
        public static void Main(string[] args)
        {
            //是输入一串数组
            Console.WriteLine("请出入一串整数:");
            string num = Console.ReadLine();
            string[] arryNum;
            arryNum = num.Split(new char[] { ' ' });
            int[] intNum = new int[arryNum.Length];
            int i = 0;
            foreach (string s in arryNum)
            {
                intNum[i++] = Convert.ToInt32(s);
            }

            Array.Sort(intNum);

            Console.WriteLine("请输入要查找的数:");
            int findnum = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("数{0}的位置是:{1}", findnum, Search(intNum, findnum));
            Console.ReadKey();

 

            //确定的一串数组;

            //int[] arryint = new int[] { 1, 3, 5, 7, 9, 10, 14, 18 };
            //int j = Search(arryint, 9);
            //Console.WriteLine("所查找的数的位置为:{0}", j);
            //Console.ReadKey();

        }
        static int Search(int[] data, int key)
        {
            int low = 0;
            int high = data.Length - 1;
            while (low <= high)
            {
                int middle = (low + high) / 2;
                if (data[middle] == key)
                {
                    return middle + 1;
                }
                else if (data[middle] < key)
                {
                    low = middle + 1;
                }
                else
                {
                    high = middle - 1;
                }
            }
            return -1;

        }

        #region nomalSearch
        static int nomalSearch(int[] data, int key)
        {
            int i ;
            for (i=0; i < data.Length; i++)
            {
                if (data[i] == key)
                {
                    return i+1;
                   
                }             
            }
            return -1;
        }

        #endregion

    }
}

 

原创粉丝点击