C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Count The Number Of Characters Occurrence In A String/ Word
WhatsApp
Karthik Elumalai
Aug 22
2016
919
0
0
Way 1
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
ProgrammingInterviewFAQ.CharacterOccurenceCount
{
class
Way1
{
static
void
Main() {
String input =
"Programmer"
;
string
ChartoFind =
"r"
;
int
totallengthofinput = input.Length;
int
LengthOfInputWithOutCharToFind = input.Replace(ChartoFind,
""
).Length;
int
resultcount = totallengthofinput - LengthOfInputWithOutCharToFind;
Console.WriteLine(
"Number of times occured the character r occured is : {0}"
, resultcount);
Console.ReadLine();
}
}
}
Way 2
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
ProgrammingInterviewFAQ.CharacterOccurenceCount
{
class
Way2
{
static
void
Main()
{
String input =
"corner"
;
char
ChartoFind =
'r'
;
int
count = 0;
foreach
(
char
charfrominput
in
input) {
if
(charfrominput == ChartoFind) {
count++;
}
}
Console.WriteLine(
"Number of times occured the character r occured is : {0}"
, count);
Console.ReadLine();
}
}
}
Count The Number Of Characters
ASP.NET
C#
Up Next
Count The Number Of Characters Occurrence In A String/ Word