1
Console.WriteLine("Insert the character you are searching for: ");
CharacterSearch = Console.ReadLine();
var count = file.Where(x => x == CharacterSearch).Count();

So what I am trying to do is to read text from the variable file and then search for a specific character in the string that is entered from the keyboard. So the searched character would be CharacterSearch.

I also want to check for their positions not just the number of occurrences.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
xDevil
  • 147
  • 1
  • 2
  • 12
  • forgot to say that this does not show the number of aparitions because i get the error "Operator == cannot be used for Strings or Char". – xDevil Dec 06 '13 at 14:47
  • 1
    First off [How would you count occurrences of a string within a string?](http://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-within-a-string) – Liam Dec 06 '13 at 14:48
  • You can (and should) edit your question. – BartoszKP Dec 06 '13 at 14:48
  • Just get the first character from the input string, or do a ReadKey instead of a ReadLine – Panagiotis Kanavos Dec 06 '13 at 14:59

3 Answers3

1

This is very inefficient, but it's an option is the files are small.

CharacterSearch = Console.ReadKey();
var count = File.ReadLines(pathToFile)
    .Select((c, i) => new { Character = c, Index = i })
    .ToList()
    .Where(x => x.Character == CharacterSearch);
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • i get the error Char does not contain a definition of KeyChar – xDevil Dec 06 '13 at 15:05
  • Now i get the error : System.IO.StreamReader does not contain a definition of select.Keep in mind im trying to search through a text file.:( – xDevil Dec 06 '13 at 15:25
  • @user2524603, please see my edit, and get rid of that `StreamReader`. – Mike Perrenoud Dec 06 '13 at 15:29
  • Now i get no errors but the program just does nothing after this point. – xDevil Dec 06 '13 at 15:38
  • This is the file code! might have something to do with it. Console.WriteLine(Extra); int counter = 0; string line; System.IO.StreamReader file = new System.IO.StreamReader(PathS); while ((line = file.ReadLine()) != null) { Console.WriteLine(line); counter++; } – xDevil Dec 06 '13 at 15:39
  • @user2524603, well you've got to do something with the results. It's a `List` where `T` is the anonymous type created there. – Mike Perrenoud Dec 06 '13 at 15:39
  • @user2524603, get *rid of the `StreamReader`!* The `File.ReadLines` does *all the work.* – Mike Perrenoud Dec 06 '13 at 15:40
1

Why not simply use this method? (Are you looking for a complicated one?)

List<int> indexes = new List<int>();

for (int i = 0; i < file.Length; i++)
{
    if (file[i]==CharacterSearch)
    {
        indexes.Add(i);    
    }
}

int count = indexes.Count;
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
0

Inefficient but can be applied if file is small.

public static int CharCount(string input,char c)
{
    var charCount=input.GroupBy(a => a).ToDictionary(k => k.Key, v => v.Count());
    if (charCount.ContainsKey(c))
        return charCount[c];
    return 0;
}

If you want to do the search over an over in the same string, i think it is better maintain a mapping. Again considerable only for a small file.

 public static Dictionary<char, int> CharCountMapping(string input)
{
    return input.GroupBy(a => a).ToDictionary(k => k.Key, v => v.Count());
}
Sameer
  • 3,124
  • 5
  • 30
  • 57