C# Program to Find the No of Occurrence of a Word in a String
Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor.
This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.
Question:
How can you find the total number of occurrences of a given string or word in a paragraph or sentence? Write a program to find the same.
C# Program To Find the Frequency of a Word in a String
// Author : Tech Point Fundamentals
// Website : www.techpointfunda.com
// Channel : https://www.youtube.com/c/TechPointFundamentals
// Demo : Occurrence Count of a Word
using System;
public class Program
{
public static void Main()
{
string inputString = "Tech Point Fundamentals Tech Point Tech Fundamentals www.techpointfunda.com";
string inputWord = "Tech";
Console.WriteLine("Input String : " + inputString);
Console.WriteLine("Input Word : " + inputWord);
Console.WriteLine("Word Max Count : " + CharacterCount(inputString, inputWord));
}
public static int CharacterCount(string inputString, string word)
{
int count = 0;
word = word.ToLower();
string[] wordArray = inputString.ToLower().Split(' ');
foreach (string item in wordArray)
{
if (item == word)
{
count = count + 1;
}
}
return count;
}
}
Output:
Input String: Tech Point Fundamentals Tech Point Tech Fundamentals www.techpointfunda.com
Input Word : Tech
Word Max Count : 3
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.