Console Application to count all '#' characters in a text file - c#

My code is posted below. I expect it to output the number of # characters in an input file. It is currently not providing the expected output.
static void Main(string[] args)
{
StreamReader oReader;
if (File.Exists(#"C:\Documents and Settings\9chat73\Desktop\count.txt"))
{
Console.WriteLine("Enter a word to search");
string cSearforSomething = Console.ReadLine().Trim();
oReader = new StreamReader(#"C:\Documents and Settings\9chat73\Desktop\count.txt");
string cColl = oReader.ReadToEnd();
string cCriteria = #"\b" + cSearforSomething + #"\b";
System.Text.RegularExpressions.Regex oRegex = new System.Text.RegularExpressions.Regex(cCriteria, RegexOptions.IgnoreCase);
int count = oRegex.Matches(cColl).Count;
Console.WriteLine(count.ToString());
}
Console.ReadLine();
}
This is giving me output as 0 every time. I have the below file as count.txt :00100324103| #00100324137| #00100324145| #00100324153| #00100324179| . I want to calculate the number of Hashes(#) inside the file. How to do that.

You are looking for # as separate word. Remove word boundaries requirement from your criteria:
string cCriteria = cSearforSomething;

The problem is that '#' (symbol your looking for) is a special symbol in regular expressions
and so, should be escaped:
static void Main(string[] args) {
//String fileName = #"C:\Documents and Settings\9chat73\Desktop\count.txt";
// To search dinamically, just ask for a file:
Console.WriteLine("Enter a file to search");
String fileName = Console.ReadLine().Trim();
if (File.Exists(fileName)) {
Console.WriteLine("Enter a word to search");
String pattern = Console.ReadLine().Trim();
// Do not forget to escape the pattern!
int count = Regex.Matches(File.ReadAllText(fileName),
Regex.Escape(pattern),
RegexOptions.IgnoreCase).Count;
Console.WriteLine(count.ToString());
}
Console.ReadLine();
}

Try the above
int count = cColl.Count(x => x == '#');
var count = File.ReadAllText(#"c:\...").Count(x => x == '#');

string cCriteria = #"\b" + cSearforSomething + #"\b";
This is your problem. If you remove the #"\b" from each end, you'll get the correct amount of '#' characters, because those characters denote the ends of a word and the '#' character is not it's own word.

Related

How to swap first and last letters in each word?

I have a practice session on C#, and I want to know how can I swap first and last characters in each word of a sentence and lower case them. I have created a string array that represents each word, and in an inner for loop, I am iterating each character in each word. There is my code.
using System;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
string text = "Hello world";
string[] words = text.Split(" ");
string output = "";
for(int i = 0; i < words.Length; i++)
{
for(int j = 0; j < words[i].Length; j++)
{
if (char.IsUpper(words[i][j]))
{
output += char.ToLower(words[i][j]);
}
else
{
output += words[i][j];
}
}
output += " ";
}
Console.WriteLine(output);
}
}
}
Because It is sunday :-), here is the complete code (my explanations were to difficult):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string output = "ABCD EFGH IJKL";
string[] outputs = output.Split(' ');
char[] chars;
string first,last;
string flower,llower;
string result = string.Empty;
for (int i = 0; i < outputs.Length; i++)
{
chars = outputs[i].ToCharArray();
first = new string(chars[0], 1);
last = new string(chars[chars.Length - 1], 1);
flower = first.ToLower();
llower = last.ToLower();
chars[chars.Length - 1] = flower.ToCharArray()[0];
chars[0] = llower.ToCharArray()[0];
result += new string(chars);
result += " ";
}
Console.WriteLine(output);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Result: dBCa hFGe lJKi
How do I lowercase and reverse the first and last characters in each word.
Solution using a regular expresion
We could use the Split() method of String or Regex, to split on non-word characters, but then we wouldn't be able to output the correct characters between each word, unless we only split on a single character.
using System;
using System.Text.RegularExpressions;
namespace CS_Regex {
class Program {
static void Main(string[] args) {
// Match words using a regular experession
string match_word = #"(\w+)";
string match_non_word = #"([^\w]*)";
string pattern = match_non_word + match_word + match_non_word;
Regex rx = new Regex(pattern, RegexOptions.Compiled);
// Do the match on example data
string data = "Hello world";
MatchCollection matches = rx.Matches(data);
// Output the matches
foreach (Match match in matches) {
// Get the text before and after the word
string non_word_before = match.Groups[1].ToString();
string non_word_after = match.Groups[3].ToString();
// Get the matched word
string word = match.Groups[2].ToString();
// Lower case the first and last characters and swap them
string firstchar = (word.Length > 0) ? $"{char.ToLower(word[0])}" : "";
string lastchar = (word.Length > 1) ? $"{char.ToLower(word[word.Length - 1])}" : "";
string middle = (word.Length > 2) ? word.Substring(1, word.Length - 2) : "";
string newword = lastchar + middle + firstchar;
// Output the new word
Console.Write(non_word_before + newword + non_word_after);
}
} // Main
} // class
} // namespace
Output from the proposed solution
oellh dorlw
Links
Regular Expression Language - Quick Reference
Regex Class
Regex.Match Method
one idea is to convert each string into an array of chars then, for each array of caracters, get the first and last caracter and convert those caratcter into string (of one caracter) in order to use the lower function. Then replace the first and last letters with the lower caracters by inverting the 0 index with the last index, in order to swap.
Example for first letter (NO swap just for explaination):
string output = "ABCD";
char[] chars = output.ToCharArray();
string firt = new string(chars[0],1);
string lower = firt.ToLower();
string result = output.Replace(chars[0].ToString(), lower.ToString());
For the swap of the first letter to the last
Here is a complete code for first letter: result is "BCDa". For the last letter, it is the same idea
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string output = "ABCD";
char[] chars = output.ToCharArray();
string firt = new string(chars[0],1);
string lower = firt.ToLower();
chars[chars.Length-1] = lower.ToCharArray()[0];
string result = new string(chars);
Console.WriteLine(output);
Console.WriteLine(result);
Console.ReadLine();
}
}
}

last and first word in a string c#

i need to print the first and the last word in a string here is what i've tried
Console.WriteLine("please enter a string");
string str = Console.ReadLine();
string first = str.Substring(0, str.IndexOf(" "));
string last = str.Substring(str.LastIndexOf(' '),str.Length-1);
Console.WriteLine(first + " " + last);
when i run the code this massage appear
Unhandled Exception: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at ConsoleApp1.Tar13.Main() in C:\Users\User\source\repos\ConsoleApp1\ConsoleApp1\Tar13.cs:line 16
i dont know what is the problem
If this is homework, don't hand this in unless you really understand it, have done LINQ (or have a supervisor that approves of off-piste learning and you're prepared to acknowledge you got outside assistance/did background learning) and are willing to explain it if asked:
Console.WriteLine("please enter a string");
string str = Console.ReadLine();
string[] bits = str.Split();
Console.WriteLine(bits.First() + " " + bits.Last());
For a non-LINQ version:
Console.WriteLine("please enter a string");
string str = Console.ReadLine();
string first = str.Remove(str.IndexOf(' '));
string last = str.Substring(str.LastIndexOf(' ') + 1);
Console.WriteLine(first + " " + last);
Bear in mind that these will crash if there are no spaces in the string - the Split version won't
Look at String Remove and Substring
If you want to robust things up so it doesn't crash:
Console.WriteLine("please enter a string");
string str = Console.ReadLine();
if(str.Contains(" ")){
string first = str.Remove(str.IndexOf(' '));
string last = str.Substring(str.LastIndexOf(' ') + 1);
Console.WriteLine(first + " " + last);
}
I'll leave a "what might we put in an else?" in that last code block, as an exercise for you :)
you can split the string and get first and last...
var s = str.Split(' ', StringSplitOptions.RemoveEmptyEntries );
if(s.Length >= 2)
{
var first = s.First();
var last = s.Last();
Console.WriteLine($"{first} {last}");
}
In general case when sentence can contain punctuation, not necessary English letters you can try regular expressions. Let's define
Word is non empty sequence of letters and apostrophes
And so we have
Code:
using System.Linq;
using System.Text.RegularExpressions;
...
private static (string first, string last) Solve(string value) {
if (string.IsNullOrWhiteSpace(value))
return ("", "");
var words = Regex
.Matches(value, #"[\p{L}']+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
return words.Length > 0
? (words[0], words[words.Length - 1])
: ("", "");
}
Demo:
string[] tests = new string[] {
"Simple string", // Simple Smoke Test
"Single", // Single word which is both first an last
"", // No words at all; let's return empty strings
"words, punctuations: the end.", // Punctuations
"Русская (Russian) строка!", // Punctuations, non-English words
};
var result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-30} :: {Solve(test)}"));
Console.Write(result);
Outcome:
Simple string :: (Simple, string)
Single :: (Single, Single)
:: (, )
words, punctuations: the end. :: (words, end)
Русская (Russian) строка! :: (Русская, строка)
If you want to get the last and first-word try to do the following:
string sentence = "Hello World"; //Sentence
string first = sentence.Split(" ")[0]; //First word
string last = sentence.Split(" ")[sentence.Split(" ").Length -1]; //Last word
Console.WriteLine(first + " "+ last);

How to find word with punctuation after it? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
My task is to find a specific word from a line by splitting with punctuation that can be anything (even a letter or any character in general). After finding the word, I have to return the word with punctuation that goes after it. I have come up with idea to firstly split line by punctuation, find each string that goes in between punctuation marks and then to split the same line only with words, but for some reason i only get the regular expression.
public static string FindWord2InLine(string line, string punctuation)
{
string[] parts = Regex.Split(line,"[" + punctuation + "]+");
string temp = "";
for (int i = 0; i < parts.Length; i++)
{
temp += Regex.Split(line, "[" + parts[i] + "]+");
}
return temp;
}
I think the the regex you want is \b[\w-]+?\b[,.] where ,. are your punctuation characters.
To break down the regex a little more:
\b is a word boundary.
[\w-] matches a word character (letter / number) or a hyphen.
+? matches the characters/hyphens at least once but as few as possible before the next boundary
Given the input Some words here. With some, punctuation... this makes three matches:
here.
some,
punctuation.
Since you are returning only a single string from your function, quite how you determine which of these matches to return is up to you.
You can use this code:
public static string FindWord2InLine(string line, string punctuation)
{
var matches = Regex.Matches(line, $"\\w+[{punctuation}]"); //match word with punctuation after it
string temp = "";
foreach (var match in matches)
{
temp += match; // perform action with word and punctuation
}
return temp;
}
Return of calling FindWord2InLine("foo, bar!,..,/ baz.", ",.!:") is foo,bar!baz.
Found out, that " Match withPunctuation = Regex.Match(line, temp + "[" + punctuation + "]+"); " Does The job
public static string FindWord2InLine(string line, string punctuation)
{
string[] parts = Regex.Split(line, "[" + punctuation + "]+");
int max = 0;
string temp = "";
for (int i = 0; i < parts.Length; i++)
{
if (parts[i].Length > max) //If Longest Word
{
if ((parts[i].Length + 1) / 2 <= NumberOfDigits(parts[i])) // If atleast half of numbers are digits
{
temp = parts[i];
max = parts[i].Length;
}
}
}
Match withPunctuation = Regex.Match(line, temp + "[" + punctuation + "]+"); // matches line with word and punctuation
return withPunctuation.ToString();
}

How to only allow single words to be input using console.readline() in c#?

I have the following code.
Console.Clear();
string encodedMessage = "";
Console.WriteLine("Enter a word to encode");
char[] stringtoencode = Console.ReadLine().ToCharArray();
for (int i = 1; i < stringtoencode.Length; i++)
{
string currentCharAsString = stringtoencode[i].ToString();
encodedMessage += currentCharAsString;
}
encodedMessage = encodedMessage + stringtoencode[0].ToString() + "ay";
Console.WriteLine("Your string encodes to backslang as " +encodedMessage);
It takes a string input from the user and encodes it into a form of backslang (which is a phonetic encryption, which simply moves the first letter of the word to the end of the word and adds 'ay' to the end of the word)
I am using Console.ReadLine() to retrieve the input string. How can I modify the above code so that it only allows the user to enter a single word, following the prompt 'enter a word to encode'?
This will ask the user to input a (new) word if the line read contains a space.
string word;
do
{
Console.WriteLine("Enter a word to encode");
word = Console.ReadLine();
} while (word.Contains(' '));
var encodedMessage = word.Substring(1) + word[0] + "ay";
Console.WriteLine("Your string encodes to backslang as " + encodedMessage);

c# finding indexof and remove character atsign

there is a String which contains some # characters, i want to find " # " in my string and remove them, but it also finds and removes these ones: "#"
int atsignPlace = str.IndexOf(" # ");
while (atsignPlace >= 0)
{
str = str.Remove(atsignPlace,3);
atsignPlace = str.IndexOf(" # ");
}
i tried this code, but it removes nothing, so it always finds first '#' ,which makes it an infinite loop.
int atsignPlace = str.IndexOf(" #");
while (atsignPlace >= 0)
{
if( atsignPlace+1 < str.Length && str[atsignPlace+1] == ' ' )
str = str.Remove(atsignPlace,3);
atsignPlace = str.IndexOf(" # ");
}
Replace method also doesn't work correct.
str = str.Replace(" # ", String.Empty);
maybe there is a problem with '#' character.
the input string is a sql query, i am trying to remove some parameters from it.
[ i have used try-catch for exceptions ]
Your code works fine. Short but complete program to demonstrate:
using System;
class Test
{
static void Main()
{
string before = "xyz # abc#123";
string after = CustomRemove(before);
Console.WriteLine(after); // Prints xyzabc#123
}
static string CustomRemove(string text)
{
int atSignIndex = text.IndexOf(" # ");
while (atSignIndex >= 0)
{
text = text.Remove(atSignIndex, 3);
atSignIndex = text.IndexOf(" # ");
}
return text;
}
}
EDIT: Of course, Replace works fine too:
using System;
class Test
{
static void Main()
{
string before = "xyz # abc#123";
string after = before.Replace(" # ", "");
Console.WriteLine(after); // Prints xyzabc#123
}
}
If you're still seeing a problem with either of these, then the issue is in how you're using this code, not in the code itself.
One guess: you might have non-printed characters within the " # " which is preventing them from being removed. But you haven't really given us enough information to say. A short but complete program demonstrating it not working would help...
Keep it simple:
string result = input.Replace(" # ", String.Empty);
MSDN: String.Replace Method (String, String)
I would use regex to make sure that you get any number of whitespaces:
Regex.Replace(input, #"\s+#\s+", m => string.Empty);
string LclString = "#12 # 123#123 # #";
LclString = LclString.Replace(" # ", " ");
Yields this:
#12 123#123 #

Categories