在微博上看到的
要求:写一个函数将单词随机打乱
规则:单词首尾字母不变,中间的字母随机打乱例如:The cost of the citys response to the disaster has spiralled
打乱成: The csot of the ctyis rspoense to the diatsesr has salpirleadstring shuffle(string input)
{
     //给出实现
}

解决方案 »

  1.   

    先写一个打乱的方法
    然后把字符串按空格分割成数组
    然后取每个数组元素去掉首尾的子字符串
    然后用写的方法打乱顺序
    然后放回去
    然后把数组join起来
      

  2.   

    string init_str = "The cost of the citys response to the disaster has spiralled";                string result=string.Join(" ",Regex.Split(init_str, @"\s+").Select(a => { 
                        var temp_list=a.ToList();
                        char begin_char = temp_list.First();
                        char end_char = temp_list.Last();
                        temp_list.Remove(begin_char);
                        temp_list.Remove(end_char);
                        temp_list=temp_list.OrderBy(b => Guid.NewGuid()).ToList();//随机
                        return begin_char.ToString()+string.Join("",temp_list)+end_char.ToString();
                        
                    }));
      

  3.   


    string shuffle(string input)
    {
      string[] words = input.split(" ");
      Random r = new Random();
      foreach(string word in words)
      {
        for(int i=1; i<word.Length-2; i++)
        {
          int idx = r.Next(0, word.Length-2);
          char c = word[i];
          word[i] = word[idx];
          word[idx] = c;
        }
      }
      return string.Join(words);

      

  4.   

    string shuffle(string input)
    {
    Random r=new Random();
    return string.Join(" ", input.Split(' ').Select( x => x.First() + string.Join("", x.Skip(1).Take(x.Count() - 2).OrderBy(y => r.Next(-1, 1))) + x.Last()));
      

  5.   

    给出我自己的实现,一行代码Regex.Replace("hello world", @"(?<=\w)\w*(?=\w)", it => it.Value.Length < 2 ? it.Value :                new string(it.Value.OrderBy(_ => rand.Next()).ToArray()));
      

  6.   

    1.得到你单词的长度。
    2.保留你的头和尾
    3.string.ToCharArray() 中间的字符串进行 Random 下。从新组合就可以了
    4.最后在 join 下 和头尾一组合就完事了。
      

  7.   


    private static string Shuffle(string s)
    {
        if (string.IsNullOrWhiteSpace(s) || s.Length <= 3)
        {
            return s;
        }
        var b = s.Length - 1;
        var a = new List<int>();
        var r = new Random();
        while (a.Count < s.Length - 2)
        {
            var v = r.Next(1, b);
            if (!a.Contains(v))
            {
                a.Add(v);
            }
        }
        var ns = s[0].ToString();
        foreach (var i in a)
        {
            ns += s[i];
        }
        ns += s[s.Length - 1];
        return ns;
    }
      

  8.   


    void Main()
    {
    string str="The cost of the citys response to the disaster has spiralled";
     
    str=string.Join(" ",(from s in str.Split(' ')
        let m=s.ToCharArray() 
    let n=m.Count()>3?m.Skip(1).Take(m.Count()-2).OrderBy(x=>Guid.NewGuid()).ToArray():null
    select n==null?s:string.Concat(m.First(),new string(n),m.Last())).ToArray());
    }
      

  9.   

    我也想这么写来着,没想到用 GUID 排序。~~
      

  10.   

    string source = "The cost of the citys response to the disaster has spiralled";         
    var result=  string.Join(" ",source.Split(' ').Select(p =>p.Length>3?string.Concat(p.First(),new string( p.Skip(1).Take(p.Length-2).OrderBy(c=>Guid.NewGuid()).ToArray()), p.Last()):p));
      

  11.   

    保留单词头部和尾部字母,使用库函数next_permutation(p,p+n)就可以了
      

  12.   


    这就是传说中的linq技术吗?这是传说中的正则表达式吗?
      

  13.   

    笨人写的   不过可以一用 结果是楼主想要的
     string _str = "The cost of the citys response to the disaster has spiralled"; 
              //以空格为分界点 把每个单词放到数组中
                string [] str =  _str.Split(' ');
                int num = 0,count = 0;
                int  []s ={0,str.Length-1};
                Random r = new Random();
                for (int i = 0; i < str.Length - 1; i++)
                {
                    Console.Write(str[num] + "  ");
                    count++;
                    if (count >= 1 && count < str.Length - 1)
                    {
                    my_loop:
                        num = r.Next(str.Length);
                        if (num == 0 || num == str.Length - 1) goto my_loop;
                        else
                        {
                            for (int j = 0; j < s.Length; j++)
                            {
                                if (s[j] == num) goto my_loop;
                            }
                            Array.Resize(ref s, s.Length + 1);
                            s[s.Length - 1] = num;
                        }
                    }
                    else Console.WriteLine(str[str.Length - 1]);
                }