Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
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
Get Count of Occurrence of Characters in String using C#
WhatsApp
Govinda Rajulu Yemineni
Jul 11
2015
1.5
k
0
0
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
Up Next
Get Count of Occurrence of Characters in String using C#