You must Sign In to post a response.
  • Category: .NET

    About C# regarding string

    1) How can i count the repetition of word in a string in c#
    2)How can i count the repetition of a character in string in c#
  • #766420
    1) How can i count the repetition of word in a string in c#
    Simple, get a string array, pass words to it and use below code snippet

    using System;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication16
    {
    class Program
    {
    static readonly string[] Tests = { "abcdabcd", "xabcdabcd", "abcdabc", "xaaabcdabcd" };
    static readonly Regex FindDup = new Regex(@"(.+)\1", RegexOptions.IgnoreCase);
    static void Main(string[] args)
    {
    foreach (string t in Tests)
    {
    MatchCollection allMatches = FindDup.Matches(t);
    Trace.WriteLine(string.Format("{0}: {1}", t, allMatches.Count));
    }
    }
    }
    }

    //output
    abcdabcd: 1
    xabcdabcd: 1
    abcdabc: 0
    xaaabcdabcd: 2


    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766437
    Hi,

    We can achieve this in no of ways but for maintain uniqueness of the code my suggestion is create one separate method for counting string and call the same method while pass the input word from user end.

    Refer below link, in this example they made the same, they make one method and call the same method while passing input from user end.

    http://www.dotnetperls.com/string-occurrence

    If you still have doubts please refer below link https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=count%20repeated%20words%20in%20string%20using%20c%23

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766446
    Hi,
    Try this:

    using System.Linq;

    string szInputStr = "hello friend, its a demo to find occurence of characters";
    char cSearchChar = 'r';
    int iFinalCharCount = szInputStr.Where(chVar => chVar == cSearchChar).Count();
    Console.WriteLine("Repetition of a character '" + cSearchChar + "' in string: " + iFinalCharCount);

    string[] szArrInput = szInputStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    string szSearchStr = "demo";
    var iFinalWordCount = (from eachWord in szArrInput
    where eachWord.ToUpperInvariant() == szSearchStr.ToUpperInvariant()
    select eachWord).Count();
    Console.WriteLine("Repetition of word '" + szSearchStr + "' in a string: " + iFinalWordCount);

  • #766501
    you can try following ...
    string searchText = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
    string regexPattern = @"\btrue\b";
    int numberOfTrues = Regex.Matches(searchText, regexPattern).Count;

    OR

    numberOfTrues = Regex.Matches(searchText, "true").Count;

  • #766549
    Hi,,

    1.How can i count the repetition of word in a string in c#

    using System;
    using System.Collections.Generic;
    using System.Text;
    using UBS.TextParsing.Interfaces;

    namespace UBS.TextParsing
    {
    public class StringSplitter : IStringSplitter
    {
    public IEnumerable<string> SplitIntoWords(string sentence)
    {
    Guard.NotNull(() => sentence, sentence);

    var stringList = new List<string>();

    var currentWordSb = new StringBuilder();

    foreach (var chr in sentence)
    {
    if (Char.IsPunctuation(chr) || Char.IsSeparator(chr) || Char.IsWhiteSpace(chr))
    {
    AddToList(currentWordSb, stringList);
    currentWordSb.Clear();
    }
    else
    {
    currentWordSb.Append(chr);
    }
    }

    AddToList(currentWordSb, stringList);

    return stringList;
    }

    private void AddToList(StringBuilder stringBuilder, ICollection<string> collection)
    {
    var word = stringBuilder.ToString();
    if(!string.IsNullOrEmpty(word))
    collection.Add(word);
    }
    }
    }

  • #766550
    HI,,

    Ans For 2nd question,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SchoolDays
    {
    class Program
    {
    static void Main(string[] args)
    {
    string input = "I Love India";

    while (input.Length > 0)
    {
    Console.Write(input[0] + " : ");
    int count = 0;
    for (int j = 0; j < input.Length; j++)
    {
    if (input[0] == input[j])
    {
    count++;
    }
    }
    Console.WriteLine(count);
    input = input.Replace(input[0].ToString(),string.Empty);
    }
    Console.ReadLine();
    }
    }
    }

  • #766589
    You can do this using the "IndexOf" method to do this. You can do this using simple while/for loop.

    string MyText = "This is my test in the test appliction";
    string FindString = "test";
    // Loop through all instances of the string 'text'.
    int count = 0;
    int i = 0;
    while ((i = MyText.IndexOf(FindString , i)) != -1)
    {
    i += FindString .Length;
    count++;
    }
    Console.WriteLine(count.tosString());

    In the above code you are searching the word from the string(text) by using the "IndexOf" still you reach the last finding of the give word.

    By Nathan
    Direction is important than speed

  • #766595
    Hi

    you can try this code



    //Character
    string st = "India is Big Country";
    int cnt = 0;
    for (int i = 0; i <= st.Count() - 1; i++)
    {
    if (st[i].ToString() == "I")
    {
    cnt = cnt + st[i];
    }
    }


    //Word

    string st1 = "India is Big Country";
    int cnt1 = 0;
    for (int i = 0; i <= st1.ToString().Split(' ').Count() - 1; i++)
    {
    if (st1[i].ToString() == "India")
    {
    cnt1 = cnt1 + st1[i];
    }
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766869
    You can use CharacterEnumerator

    the below is the code for this

    string title = "Dotnetspider is code based";
    CharEnumerator chEnum = title.GetEnumerator();
    int ctr = 1;
    string outputLine1 = null;
    string outputLine2 = null;
    string outputLine3 = null;

    while (chEnum.MoveNext())
    {
    outputLine1 += ctr < 10 || ctr % 10 != 0 ? " " : (ctr / 10) + " ";
    outputLine2 += (ctr % 10) + " ";
    outputLine3 += chEnum.Current + " ";
    ctr++;
    }

    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


Sign In to post your comments