Scr1pt25
Scr1pt25

Reputation: 57

How to insert a string every n words?

I need help with text. I got a code which for example finds if the line has even number of words, then it finds every 2nd word in a text file. The problem is i don't know how to append a string to that every 2nd word and print it out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace sd
{
class Program
{
    const string CFd = "..\\..\\A.txt";
    const string CFr = "..\\..\\Rezults.txt";

    static void Main(string[] args)
    {


        Apdoroti(CFd, CFr);
        Console.WriteLine();

        }
    static void Apdoroti(string fd, string fr)
    {
        string[] lines = File.ReadAllLines(fd, Encoding.GetEncoding(1257));
        using (var far = File.CreateText(fr))
        {
                StringBuilder news = new StringBuilder();
                VD(CFd,news);
                far.WriteLine(news);

        }
    }


    static void VD(string fv, StringBuilder news)
    {
        using (StreamReader reader = new StreamReader(fv,
        Encoding.GetEncoding(1257)))
        {
            string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
            int nrl;
            int prad = 1;
            foreach (string line in lines)
            {
                nrl = line.Trim().Split(' ').Count();
                string[] parts = line.Split(' ');
                if (nrl % 2 == 0)
                {
                    Console.WriteLine(nrl);
                    for (int i = 0; i < nrl; i += 2)
                    {
                        int ind = line.IndexOf(parts[i]);
                        nauja.Append(parts[i]);
                        Console.WriteLine(" {0} ", news);
                    }

                }
            }
        }
    }

}

}

For example if i got a text like: "Monster in the Jungle Once upon a time a wise lion lived in jungle. He was always respected for his intelligence and kindness."

Then it should print out: "Monster in abb the Jungle abb Once upon abb a time abb a wise abb lion lived abb in jungle. He was always respected for his intelligence and kindness."

Upvotes: 2

Views: 146

Answers (2)

peeyush singh
peeyush singh

Reputation: 1407

You can use linq, first parse the line on spaces to get a list of words (you are doing) and then for every odd element add the text required, finally convert the array back into a string.

    string test = "Monster in the Jungle Once upon a time a wise lion lived in jungle. He was always respected for his intelligence and kindness.";
    var words = test.Split(' ');
    var wordArray = words.Select((w, i) => 
        (i % 2 != 0) ? (w+ " asd ")   : (w + " ")
    ).ToArray();

    var res = string.Join("", wordArray);

Also this can be easily changed to insert after every n words by changing the mod function. Do remember that array index will start at 0 though.

Upvotes: 0

Poul Bak
Poul Bak

Reputation: 10940

You can do it with a regex replace, like this regex:

@"\w+\s\w+\s"

It maches a Word, a Space, a Word and a Space.

Now replace it with:

"$&abb "

How to use:

using System.Text.RegularExpressions;

string text =  "Monster in the Jungle Once upon a time a wise lion lived in jungle. He was always respected for his intelligence and kindness.";
Regex regex = new Regex(@"\w+\s\w+\s");
string output = regex.Replace(text, "$&abb ");

Now you will get the desired output.

Edit:

To Work with any number of Words, you can use:

@"(\w+\s){3}"

where the quantifier (here 3) can be set to whatever you want.

Edit2:

If you don't want to replace numbers:

@"([a-zA-Z]+\s){2}"

Upvotes: 1

Related Questions