Interviews5 answers
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 ?
5 Answers
string str = "Welcome to Programming World";
int result = str.ToCharArray().Count(c => c == 'r');
Sweta ShetyePosted Dec 4, 2016, 9:04 PM EST
string str = "Welcome to programming world";
int charcount3 = str.Length - str.Replace("r", string.Empty).Length;
Console.WriteLine(charcount3);
Gaurav MishraPosted May 23, 2016, 11:36 AM EDT
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();
}
}
}
Gaurav MishraPosted May 23, 2016, 11:12 AM EDT
int count=Regex.Matches(STR, "r").Count;
Srinivasulu ThottempudiPosted Mar 31, 2016, 11:26 PM EDT
Try this
string _str = "Welcome to PRogramming world";
var charCount = _str.ToCharArray().Where(item => item.ToString().ToLower() == "r").Count();
Sankar ManoharanPosted Feb 16, 2016, 9:00 PM EST

