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
Get Count of Occurrence of Characters in String using C#
Govinda Rajulu Yemineni
Jul 11
2015
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
void
CharCount()
{
string
str = Console.ReadLine();
int
length = str.Length;
char
[] charArr =
new
char
[length];
for
(
int
i = 0; i < length; i++)
//Converting String to Char Array
{
charArr[i] = str[i];
}
for
(
int
i = 0; i < length; i++)
{
bool
flag =
false
;
for
(
int
k = i - 1; k >= 0; k--)
//Checking whether the current char already repeated or not
{
if
(
char
.ToUpper(charArr[i]) ==
char
.ToUpper(charArr[k]))
{
flag =
true
;
}
}
if
(!flag)
{
int
count = 1;
for
(
int
j = i+1; j < length; j++)
{
if
(
char
.ToUpper(charArr[i]) ==
char
.ToUpper(charArr[j]))
{
count++;
}
}
Console.WriteLine(
char
.ToLower(charArr[i]) +
"="
+ count);
}
}
}
String
C#
Count of Occurrence of Characters