We have a string STR = “Welcome to Programming world.” Can you write a program to find out number of ‘r’ char count without using for loop ?
Akash Varshney
string str = "Welcome to Programming World";int result = str.ToCharArray().Count(c => c == 'r');
int count=Regex.Matches(STR, "r").Count;
string str = "Welcome to programming world"; int charcount3 = str.Length - str.Replace("r", string.Empty).Length; Console.WriteLine(charcount3);
Both the answers posted below are correct. using System; using System.Linq; using System.Text.RegularExpressions;namespace DemoConsoleApp {class Program{static void Main(string[] args){string str = "Welcome to programming world";int count = Regex.Matches(str, "r").Count;Console.WriteLine(count);var charCount = str.ToCharArray().Where(item => item.ToString().ToLower() == "r").Count();Console.WriteLine(charCount);Console.ReadKey();}} }
Try thisstring _str = "Welcome to PRogramming world";var charCount = _str.ToCharArray().Where(item => item.ToString().ToLower() == "r").Count();