How to insert a string every n words?











up vote
2
down vote

favorite
1












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."










share|improve this question
























  • It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
    – maccettura
    Nov 19 at 18:41










  • ill give my full code
    – Scr1pt25
    Nov 19 at 18:46










  • I think you mean even number of words.
    – paparazzo
    Nov 19 at 19:07















up vote
2
down vote

favorite
1












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."










share|improve this question
























  • It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
    – maccettura
    Nov 19 at 18:41










  • ill give my full code
    – Scr1pt25
    Nov 19 at 18:46










  • I think you mean even number of words.
    – paparazzo
    Nov 19 at 19:07













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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."










share|improve this question















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."







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:17

























asked Nov 19 at 18:34









Scr1pt25

304




304












  • It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
    – maccettura
    Nov 19 at 18:41










  • ill give my full code
    – Scr1pt25
    Nov 19 at 18:46










  • I think you mean even number of words.
    – paparazzo
    Nov 19 at 19:07


















  • It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
    – maccettura
    Nov 19 at 18:41










  • ill give my full code
    – Scr1pt25
    Nov 19 at 18:46










  • I think you mean even number of words.
    – paparazzo
    Nov 19 at 19:07
















It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
– maccettura
Nov 19 at 18:41




It is so hard to follow your existing code. What does VD() mean? What does fv mean? You really need to work on your naming
– maccettura
Nov 19 at 18:41












ill give my full code
– Scr1pt25
Nov 19 at 18:46




ill give my full code
– Scr1pt25
Nov 19 at 18:46












I think you mean even number of words.
– paparazzo
Nov 19 at 19:07




I think you mean even number of words.
– paparazzo
Nov 19 at 19:07












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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



@"w+sw+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+sw+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}"





share|improve this answer























  • Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
    – Scr1pt25
    Nov 19 at 19:17


















up vote
0
down vote













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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380717%2fhow-to-insert-a-string-every-n-words%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










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



    @"w+sw+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+sw+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}"





    share|improve this answer























    • Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
      – Scr1pt25
      Nov 19 at 19:17















    up vote
    1
    down vote



    accepted










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



    @"w+sw+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+sw+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}"





    share|improve this answer























    • Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
      – Scr1pt25
      Nov 19 at 19:17













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






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



    @"w+sw+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+sw+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}"





    share|improve this answer














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



    @"w+sw+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+sw+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}"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 2:56

























    answered Nov 19 at 18:49









    Poul Bak

    5,42831132




    5,42831132












    • Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
      – Scr1pt25
      Nov 19 at 19:17


















    • Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
      – Scr1pt25
      Nov 19 at 19:17
















    Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
    – Scr1pt25
    Nov 19 at 19:17




    Thank you a lot, but what if there are numbers in the text, how can you make it so that it would skip them?
    – Scr1pt25
    Nov 19 at 19:17












    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 3:56









        peeyush singh

        287210




        287210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380717%2fhow-to-insert-a-string-every-n-words%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'