How to check repeated letters in a string c#

前端 未结 10 529
太阳男子
太阳男子 2020-12-21 01:39

I am creating a program that checks repeated letters in a string.

For Example:

wooooooooooow
happpppppppy

This is my

相关标签:
10条回答
  • 2020-12-21 01:43

    You're running your loop one iteration too long.

    Alternatively, you could use LINQ to find the unique (distinct) characters in the word, then check their occurrences in the word. If it appears more than once, do something with it.

    void RepeatedLetters()
    {
        string word = "wooooooow";
        var distinctChars = word.Distinct();
        foreach (char c in distinctChars)
            if (word.Count(p => p == c) > 1)
            { 
                // do work on c
            }
    }
    
    0 讨论(0)
  • 2020-12-21 01:48

    To find duplicate or repeated letters in given string using C#

    string str = "Welcome Programming";
    char[] Array = str.ToCharArray();
    var duplicates = Array.GroupBy(p => p).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
    string duplicateval= string.Join(",", duplicates.ToArray());
    

    Output:

    e,o,m,r,g

    0 讨论(0)
  • 2020-12-21 01:49

    Just "remember" the last letter i would say.

    string repeatedWord = "woooooooow";
    if (string.IsNullOrEmpty( repeatedWord))
        // empty. return, throw whatever.
    
    char previousLetter = repeatedWord[0]; 
    for (int i = 1; i < repeatedWord.Count(); i++)
    {
        if (repeatedWord[i] == previousLetter)
        {
            // ....              
        }
        else
        previousLetter = repeatedWord[i];
    }
    
    0 讨论(0)
  • 2020-12-21 01:50

    run the loop until repeatedWord.Count()-1

    0 讨论(0)
  • 2020-12-21 01:53

    Another option would be using a Regex that matches repeating characters. Then, for each match, you can obtain the number of characters by using the Length property.

    string input = "wooooooow happppppppy";
    var matches = Regex.Matches(input, @"(.)\1+");
    for (int i = 0; i < matches.Count; i++)
    {
        Console.WriteLine("\"" + matches[i].Value + "\" is " + matches[i].Length + " characters long.");
        //...
    }
    Console.Read();
    
    0 讨论(0)
  • 2020-12-21 02:01

    Regular Expression:

    Regex rxContainsMultipleChars = new Regex( @"(?<char>.)\k<char>" , RegexOptions.ExplicitCapture|RegexOptions.Singleline ) ;
    .
    .
    .
    string myString = SomeStringValue() ;
    bool containsDuplicates = rxDupes.Match(myString) ;
    

    or Linq

    string s = SomeStringValue() ;
    bool containsDuplicates = s.Where( (c,i) => i > 0 && c == s[i-1] )
                               .Cast<char?>()
                               .FirstOrDefault() != null
                               ;
    

    or roll yer own:

    public bool ContainsDuplicateChars( string s )
    {
      if ( string.IsNullOrEmpty(s) ) return false ;
    
      bool containsDupes = false ;
      for ( int i = 1 ; i < s.Length && !containsDupes ; ++i )
      {
        containsDupes = s[i] == s[i-1] ;
      }
    
      return containsDupes ;
    }
    

    Or even

    public static class EnumerableHelpers
    {
      public static IEnumerable<Tuple<char,int>> RunLengthEncoder( this IEnumerable<char> list )
      {
        char? prev  = null ;
        int   count = 0 ;
    
        foreach ( char curr in list )
        {
          if      ( prev == null ) { ++count ; prev = curr ; }
          else if ( prev == curr ) { ++count ;               }
          else if ( curr != prev )
          {
            yield return new Tuple<char, int>((char)prev,count) ;
            prev = curr ;
            count = 1 ;
          }
        }
      }
    }
    

    With this last one...

    bool hasDupes = s.RunLengthEncoder().FirstOrDefault( x => x.Item2 > 1 ) != null ;
    

    or

    foreach (Tuple<char,int> run in myString.RunLengthEncoder() )
    {
      if ( run.Item2 > 1 )
      {
         // do something with the run of repeated chars.
      }
    }
    
    0 讨论(0)
提交回复
热议问题