Every word longer than 3 letter starts with capitalized + first line is Upper case – quick question

huangapple go评论57阅读模式
英文:

Every word longer than 3 letter starts with capitalized + first line is Upper case - quick question

问题

我有任务要完成,首行要大写,其余每个长度为3或更多字符的单词以大写字母开头。

我在控制台中测试它,所以它部分由StreamReader/StreamWriter创建,但当它正常运行时,我会进行修正。

问题是在与之前的for循环之前的Console.WriteLine();,它会在第一行后面产生一个空行,但没有它,它不会生成第三行。输入文本如下所示:

文本标题
sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

使用WriteLine会变成这样:

文本标题

sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

没有WriteLine会变成这样:

文本标题
Sssssss ss Ssss, ss Sssssss Ddddd Ddd Ddddd Dddddd

我认为我做得太复杂了,有点迷失,所以我正在寻找这个小问题的解决方案,或者让这段代码更简单,但现在不是重点。

using System;
using System.IO;

namespace priklad_8._4
{
    class Program
    {
        static void Main(string[] args)
        {
            firstUpper(@"aaa.txt", @"new.txt");

            Console.ReadLine();
        }
        static void firstUpper(String from, String to)
        {
            StreamReader sr = new StreamReader(from);
            StreamWriter sw = new StreamWriter(to);
            String s;
            int length = 0;
            char[] pole = new char[100];
            char dd = 'A';
            while ((s = sr.ReadLine()) != null)
            {
                if (length == 0)
                {
                    Console.WriteLine(s.ToUpper());
                    length++;
                }
                else
                {
                    String[] ss = s.Trim(' ').Split(' ');

                    Console.WriteLine();
                    for (int i = 0; i < ss.Length; i++)
                    {
                        if (ss[i].Length >= 3)
                        {
                            String ds = ss[i];
                            char[] das = ds.ToCharArray();

                            Console.Write(Char.ToUpper(das[0]) + ds.Substring(1) + " ");
                            if ((int)dd == 10)
                            {
                                Console.WriteLine();
                            }
                        }
                        else Console.Write(ss[i] + " ");
                    }
                }
            }
            sw.Close();
            sr.Close();
        }
    }
}

谢谢!

英文:

I got assignment to do first line in upper case and in the rest every word that is 3 or more characters long starts with upper case.

I am testing it in console so its made partly with streamreader/streamwriter, but when it functions well I will correct it.

Problem is that the Console.WriteLine(); before for loop with it, it makes an empty line after the first line but without it, it does not make 3rd line. The input text looks like this

title of text
sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

with the WriteLine it ends up like this:

TITLE OF TEXT

sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

and without:

TITLE OF TEXT
Sssssss ss Ssss, ss Sssssss Ddddd Ddd Ddddd Dddddd

I think I made it too complicated and got kinda lost so I am looking for the solution to this small problem or also to make this code easier but thats not the point right now.

using System;
using System.IO;

namespace priklad_8._4
{
    class Program
    {
        static void Main(string[] args)
        {
            firstUpper(@&quot;aaa.txt&quot;, @&quot;new.txt&quot;);

            Console.ReadLine();
        }
        static void firstUpper(String from, String to)
        {
            StreamReader sr = new StreamReader(from);
            StreamWriter sw = new StreamWriter(to);
            String s;
            int length = 0;
            char[] pole = new char[100];
            char dd = &#39;A&#39;;
            while ((s = sr.ReadLine()) != null)
            {
                if (length == 0)
                {
                    Console.WriteLine(s.ToUpper());
                    length++;
                }
                else
                {
                    String[] ss = s.Trim(&#39; &#39;).Split(&#39; &#39;);

                    Console.WriteLine();
                    for (int i = 0; i &lt; ss.Length; i++)
                    {
                        if (ss[i].Length &gt;= 3)
                        {
                            String ds = ss[i];
                            char[] das = ds.ToCharArray();

                            Console.Write(Char.ToUpper(das[0]) + ds.Substring(1) + &quot; &quot;);
                            if ((int)dd == 10)
                            {

                                Console.WriteLine();

                            }
                        }
                        else Console.Write(ss[i] + &quot; &quot;);
                    }
                }
            }
            sw.Close();
            sr.Close();
        }
    }
}

Thank you!

答案1

得分: 3

如果你想保留其他部分不变,只需将

Console.WriteLine(s.ToUpper());

替换为

Console.Write(s.ToUpper())

英文:

If you want to leave everything else as it is, just instead of

Console.WriteLine(s.ToUpper());

use this:

Console.Write(s.ToUpper())

答案2

得分: 1

也许有一个更清晰的方法来实现:

using (var input = new StreamReader("input.txt"))
{
    var currentLine = 0;
    while (!input.EndOfStream)
    {
        var line = input.ReadLine() ?? "";
        if (++currentLine == 1)
        {
            // 第一行转换为大写
            Console.WriteLine(line.ToUpper());
        }
        else
        {
            // 每个长度大于3的单词首字母大写
            Console.WriteLine(Regex.Replace(line, @"\w{3,}", 
                (match) => CultureInfo.CurrentCulture
                    .TextInfo.ToTitleCase(match.Value.ToLower())));
        }
    }
}
英文:

Maybe a cleaner way to get there:

using (var input = new StreamReader(&quot;input.txt&quot;))
{
    var currentLine = 0;
    while (!input.EndOfStream)
    {
        var line = input.ReadLine() ?? &quot;&quot;;
        if (++currentLine == 1)
        {
            // first line is upper case
            Console.WriteLine(line.ToUpper());
        }
        else
        {
            // Every word longer than 3 letter starts with capitalized
            Console.WriteLine(Regex.Replace(line, @&quot;\w{3,}&quot;, 
                (match) =&gt; CultureInfo.CurrentCulture
                    .TextInfo.ToTitleCase(match.Value.ToLower())));
        }
    }
}

答案3

得分: 1

static void Main(string[] args)
{
var lines = File.ReadAllLines("text.txt");
var newLines = new string[lines.Count()];

for (var i = 0; i < lines.Count(); i++)
{
    if (i == 0)
    {
        newLines[i] = lines[i].ToUpper();
    }
    else
    {
        foreach (var word in lines[i].Split(' '))
        {
            newLines[i] += string.Format("{0} ", word.Length >= 3 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word) : word);
        }
    }
}

File.WriteAllLines("newText.txt", newLines);

}

And i mean, if you wanted a very unclean looking one liner because i was bored.. You could do this.

static void Run(string fromFile, string toFile)
{
File.WriteAllLines(toFile, File.ReadAllLines(fromFile).Select((line, index) => index == 0 ? line.ToUpper() : Regex.Replace(line, @"\w{3,}", (match) => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(match.Value))));
}

英文:

Another Cleaner way to get there making use of File.ReadAllLines, and File.WriteAllLines

static void Main(string[] args)
{
    var lines = File.ReadAllLines(&quot;text.txt&quot;);
    var newLines = new string[lines.Count()];

    for (var i = 0; i &lt; lines.Count(); i++)
    {
        if (i == 0)
        {
            newLines[i] = lines[i].ToUpper();
        }
        else
        {
            foreach (var word in lines[i].Split(&#39; &#39;))
            {
                newLines[i] += string.Format(&quot;{0} &quot;, word.Length &gt;= 3 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word) : word);
            }
        }
    }

    File.WriteAllLines(&quot;newText.txt&quot;, newLines);
}

And i mean, if you wanted a very unclean looking one liner because i was bored.. You could do this.

static void Run(string fromFile, string toFile)
{
    File.WriteAllLines(toFile, File.ReadAllLines(fromFile).Select((line, index) =&gt; index == 0 ? line.ToUpper() : Regex.Replace(line, @&quot;\w{3,}&quot;, (match) =&gt; CultureInfo.CurrentCulture.TextInfo.ToTitleCase(match.Value))));
}

huangapple
  • 本文由 发表于 2020年1月4日 00:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定