简体   繁体   中英

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

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

Thank you!

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

Console.WriteLine(s.ToUpper());

use this:

Console.Write(s.ToUpper())

Maybe a cleaner way to get there:

using (var input = new StreamReader("input.txt"))
{
    var currentLine = 0;
    while (!input.EndOfStream)
    {
        var line = input.ReadLine() ?? "";
        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, @"\w{3,}", 
                (match) => CultureInfo.CurrentCulture
                    .TextInfo.ToTitleCase(match.Value.ToLower())));
        }
    }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM