C
C#13mo ago
DROP A DRACO

✅ text document boring thing but i'm stuck >_<

Hey guys, I'm trying to code a program in C# that takes a .txt as input and turns only the second column into lowercase The .txt is formatted like shown in the picture, but basically it is a couple tab spaces followed by a string in double quotes followed by another string in double quotes with some spaces seperating them. Currently my code converts both columns into lowercase and I have no idea what I'm doing wrong O_o I attached my input document and what it comes out as. Code is below:
4 Replies
DROP A DRACO
DROP A DRACOOP13mo ago
c#
using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
// Define the input and output file paths
string inputFile = "input.txt";
string outputFile = "output.txt";

// Read the input file
string[] lines = File.ReadAllLines(inputFile);

// Process and write to the output file
using (StreamWriter writer = new StreamWriter(outputFile))
{
foreach (string line in lines)
{
// Split the line based on the first occurrence of a double quote
int firstQuoteIndex = line.IndexOf('"');
if (firstQuoteIndex >= 0)
{
string firstPart = line.Substring(0, firstQuoteIndex);
string secondPart = line.Substring(firstQuoteIndex);

// Convert the second part (inside double quotes) to lowercase
string modifiedSecondPart = ConvertToLowerCaseInsideQuotes(secondPart);

// Combine the first part and the modified second part
string modifiedLine = firstPart + modifiedSecondPart;

// Write the modified line to the output file
writer.WriteLine(modifiedLine);
}
else
{
// If there's no double quote, write the line as is
writer.WriteLine(line);
}
}
}

Console.WriteLine("Conversion completed. Check the output file for the result.");

}
c#
using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
// Define the input and output file paths
string inputFile = "input.txt";
string outputFile = "output.txt";

// Read the input file
string[] lines = File.ReadAllLines(inputFile);

// Process and write to the output file
using (StreamWriter writer = new StreamWriter(outputFile))
{
foreach (string line in lines)
{
// Split the line based on the first occurrence of a double quote
int firstQuoteIndex = line.IndexOf('"');
if (firstQuoteIndex >= 0)
{
string firstPart = line.Substring(0, firstQuoteIndex);
string secondPart = line.Substring(firstQuoteIndex);

// Convert the second part (inside double quotes) to lowercase
string modifiedSecondPart = ConvertToLowerCaseInsideQuotes(secondPart);

// Combine the first part and the modified second part
string modifiedLine = firstPart + modifiedSecondPart;

// Write the modified line to the output file
writer.WriteLine(modifiedLine);
}
else
{
// If there's no double quote, write the line as is
writer.WriteLine(line);
}
}
}

Console.WriteLine("Conversion completed. Check the output file for the result.");

}
c#
// Helper function to convert text inside double quotes to lowercase
private static string ConvertToLowerCaseInsideQuotes(string text)
{
int startQuoteIndex = text.IndexOf('"');
int endQuoteIndex = text.LastIndexOf('"');
if (startQuoteIndex >= 0 && endQuoteIndex >= 0 && startQuoteIndex < endQuoteIndex)
{
string insideQuotes = text.Substring(startQuoteIndex, endQuoteIndex - startQuoteIndex + 1);
string lowercaseInsideQuotes = insideQuotes.ToLower();
return text.Substring(0, startQuoteIndex) + lowercaseInsideQuotes + text.Substring(endQuoteIndex + 1);
}
return text;
}
}
c#
// Helper function to convert text inside double quotes to lowercase
private static string ConvertToLowerCaseInsideQuotes(string text)
{
int startQuoteIndex = text.IndexOf('"');
int endQuoteIndex = text.LastIndexOf('"');
if (startQuoteIndex >= 0 && endQuoteIndex >= 0 && startQuoteIndex < endQuoteIndex)
{
string insideQuotes = text.Substring(startQuoteIndex, endQuoteIndex - startQuoteIndex + 1);
string lowercaseInsideQuotes = insideQuotes.ToLower();
return text.Substring(0, startQuoteIndex) + lowercaseInsideQuotes + text.Substring(endQuoteIndex + 1);
}
return text;
}
}
mtreit
mtreit13mo ago
Do you know how to debug code? I think if you were to step through the code in a debugger you would be able to inspect your variables and see what is going wrong.
string firstPart = line.Substring(0, firstQuoteIndex);
string secondPart = line.Substring(firstQuoteIndex);
string firstPart = line.Substring(0, firstQuoteIndex);
string secondPart = line.Substring(firstQuoteIndex);
This part is probably not doing what you think it's doing, @drop a draco You have a string that looks something like this: \t\t"Something"\t\t"OtherThing" So you find the index of the first double quote. Index 2. Then you take that many characters (2) starting at index 0. That gives you the two tab characters. Then you take everything else. That gives you the entire rest of the string minus the two starting tab characters. Then you lowercase that.
DROP A DRACO
DROP A DRACOOP13mo ago
haha, not much i use vs studio for mac and i only started about 2 months ago thank you very much sir :)
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server