Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,im kinda lost on how to work with text files. Do you guys have any examples on how to insert a string every 2 words? Like if a line has an equal number of words then in that line i insert a string every 2 words?

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

namespace _U_5_6
{
    class Program
    {
        const string CFd = "..\\..\\A.txt";
        const string CFr = "..\\..\\Rezultatai.txt";
        static void Main(string[] args)
        {
            int nr;
            Read(CFd, out nr);
            ReadWrite(CFd, CFr, " asdas", nr);
        }

        static void Read(string fv, out int nr)
        {
            string[] lines = File.ReadAllLines(fv, Encoding.GetEncoding(1257));
            nr = 0;
            int nreil;
           foreach (string line in lines)
            {
                nreil = line.Trim().Split(' ').Count();
                if ((line.Length % 2) == 0)
                {
                }
               

            }
        }

        static void ReadWrite(string fs, string fr, string eil, int n)
        {
            using (var frr = File.CreateText(fr))
            {
                using (StreamReader reader = new StreamReader(fs,
                Encoding.GetEncoding(1257)))
                {
                    string line;
                    for (int i = 0; i <= n && ((line = reader.ReadLine()) != null); i++)
                        frr.WriteLine(line+eil);
                    while ((line = reader.ReadLine()) != null)
                        frr.WriteLine(line);
                }
            }
        }
    }
}


What I have tried:

Im lost with this text bs i need some help
Posted
Updated 17-Nov-18 5:06am

1 solution

First split each line, so that each word is in a separate string.
Count the number of words.
If it's even use a for loop to process it in pairs:
C#
for (int i = 0; i < numberOfWords; i += 2)
   {
   ...
   }
Inside the loop, add the word at indexes i and i + 1 to the output. Add your string as well.
 
Share this answer
 
v2
Comments
Member 13975230 17-Nov-18 11:31am    
By saying add the word at indexes, could you explain a bit more. Do you mean like "word.Insert(i,...)"?
OriginalGriff 17-Nov-18 11:38am    
When you use string.Split, it returns an array of strings, with one split portion per array element. For example, if your input string is "123,456,789" and you split on commas:
string[] parts = "123,456,789".Split(',');
then parts will have three elements:
parts[0] = "123"
parts[1] = "456"
parts[2] = "789"
If i is zero, you can access the first two parts with indexes:
string outp = parts[i] + parts[i + 1];

Make sense?
Member 13975230 17-Nov-18 12:10pm    
Ok i got it, i have added my string as well, the problem is after i have string out, how do i replace it with the text i print out to .txt? Im lost
OriginalGriff 17-Nov-18 12:21pm    
Do you want to try that again, this time remembering that I can't see your screen, access your HDD, or read your mind? I only get exactly what you type, and so you typing as little as possible helps no-one! :laugh:
Member 13975230 17-Nov-18 12:56pm    
Nevermind i got it thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900