本帖最后由 jiasi888 于 2011-07-16 20:21:41 编辑

解决方案 »

  1.   


                string str = Console.ReadLine();
                char[] chars = str.ToCharArray();
                char[] result = new char[chars.Length + chars.Length / 2];
                int count = 0;
                for (int i = 0, j = 0; i < chars.Length - 1; )
                {
                    char c = chars[i];
                    char c2 = chars[i + 1];
                    if (c == 'n' && c2 == 'o')
                    {
                        result[j] = 'y';
                        result[j + 1] = 'e';
                        result[j + 2] = 's';
                        i += 2;
                        j += 3;
                        count += 3;
                    }
                    else
                    {
                        result[j] = c;
                        i++;
                        j++;
                        count++;
                    }
                }
                string str2 = new string(result, 0, count);
                Console.WriteLine(str2);
      

  2.   

    本帖最后由 caozhy 于 2011-07-16 21:17:20 编辑
      

  3.   

    2楼貌似编辑过了
    刚刚的代码有点问题
    noaannoannnaonoannnnanano
    yesaannoannyesannnnyes
    请按任意键继续. . .
    待俺研究下,2楼代码,1楼代码写的很棒,易懂简洁。20分+的分已经归您了
    研究完了2楼代码,结贴
      

  4.   

    您的代码可能还是有点问题,也许是您没完全理解了我的意思。
    noaanoanannoannnano
    yesaayesanannoayes
    请按任意键继续. . .
      

  5.   

    是的,编辑了下。string source, string find, string replacewith, bool wholeword
    原文本 查找文本 替换成 是否全字匹配
    最后个参数指定false
    now会替换成yesw,否则忽略。
      

  6.   

    新的代码还是有问题noaanoanannoannnano
    yesaayesanannoayes
    请按任意键继续. . .
    nno的处理很麻烦,那一下午我也是整这个的处理了您的代码具有一般性,我想学习学习麻烦您再改改?
    改这样的代码确实挺费事的。
    不知道结贴有没有时限,我11点来结吧
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(myreplace("noaanoanannoannnano", "no", "yes", true));
                Console.WriteLine("noaanoanannoannnano");
                Console.WriteLine(myreplace("noaanoanannoannnano", "no", "yes", false));
                Console.WriteLine("noaanoanannoannnano".Replace("no", "yes"));
            }        static string myreplace(string source, string find, string replacewith, bool wholeword)
            {
                int pos1 = 0;
                int pos2 = 0;
                int mpos = 0;
                string result = "";
                for (int i = 0; i < source.Length; i++)
                {                if (source[i] == find[mpos])
                    {
                        mpos++;
                    }
                    else
                    {
                        if (mpos > 0)
                        {
                            i -= mpos;
                            pos2 = i + 1;
                            mpos = 0;
                            continue;
                        }
                    }
                    if (mpos == 0) pos2++;
                    if (mpos == find.Length)
                    {
                        if (((i == find.Length - 1 || !Enumerable.Range((int)'A', 26).Union(Enumerable.Range((int)'a', 26)).Contains((int)source[i - mpos])) &&
                        (i == source.Length - 1 || !Enumerable.Range((int)'A', 26).Union(Enumerable.Range((int)'a', 26)).Contains((int)source[i + 1]))) || !wholeword)
                        {
                            result += source.Substring(pos1, pos2 - pos1) + replacewith;
                            pos1 = i + 1;
                            pos2 = i + 1;
                            mpos = 0;
                        }
                        else
                        {
                            mpos = 0;
                            pos2 += find.Length;
                        }
                    }
                }
                return result + source.Substring(pos1);
            }
        }
    }