TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Count The Number Of Characters Occurrence In A String/ Word
Karthik Elumalai
Aug 22
2016
Code
778
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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#