I am creating a program that checks repeated letters in a string.
For Example:
wooooooooooow
happpppppppy
This is my
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();
}
}
}
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);
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);
}
}
}
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;
}