How to check repeated letters in a string c#

前端 未结 10 530
太阳男子
太阳男子 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 02:01
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Delegate
    {
        class Program
        {
           public int repeatcount(string str,char ch)
            {
    
                var count = 0;
                for (int i = 0; i<str.Length; i++)
                {
                    if (ch == str[i])
                    {
                        count++;
                    }
    
                }
    
                return count;
            }
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a string");
                string str = Console.ReadLine();
                Console.WriteLine("Enter to know the reperted char");
                char ch = Convert.ToChar(Console.ReadLine());
                Program obj = new Program();
                int p=obj.repeatcount(str, ch);
                Console.WriteLine(p);
    
    
                Console.ReadLine();
    
            }
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-21 02:01

    You can change your loop condition to have the -1 (as others have already pointed out), or you can do it the cool kid way.

    var text = "wooooooooooow happpppppppy";
    var repeats = text.Zip(text.Skip(1), (a, b) => a == b).Count(x => x);
    
    0 讨论(0)
  • using System;
    
    namespace temp1
    {
        class Program
        {
            static string str = "proffession";
            static int n = str.Length;
            static string dupstr = "";
            static int cnt = 0;
            static void Main()
            {
                RepeatedCharsString(); 
            }
    
            public static void RepeatedCharsString()
            {
                for (int i = 0; i < n ; i++)
                {
                    for(int j = i + 1; j <= n-1; j++)
                    {
                        if (str[i] == str[j])
                        {
                            dupstr = dupstr + str[i];
                            cnt = cnt + 1;
                        }
                    }                
                }
                Console.WriteLine("Repeated chars are: " + dupstr);
                Console.WriteLine("No of repeated chars are: " + cnt);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 02:07
     public int RepeatedLetters(string word)
            {
                var count = 0;
                for (var i = 0; i < word.Count()-1; i++)
                {
                    if (word[i] == word[i+1])
                    {
                        count++;
                    }
                }
                return count;
            }
    
    0 讨论(0)
提交回复
热议问题