Monday, May 20, 2013
Write a method to find how many times a substring occurs in a string (C#)
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Excercises
{
class Program
{
static void Main(string[] args)
{
//Write a method to find how many times a substring occurs in a string
string find = "and";
string input = "ROME — Italy's high court on Tuesday faulted the appeals court that acquitted American student Amanda Knox of murdering her roommate, saying its ruling was full of deficiencies, contradictions and illogical conclusions and ordering the new appeals court to look at all the evidence to determine whether Knox helped kill the teen";
char[] delimiters = { ' ', ',', '.', ';', '-' };
string[] words = input.Split(delimiters);
int count = 0;
foreach (var word in words)
{
if (word.ToLower() == find.ToLower())
{
count++;
}
}
Console.WriteLine("The word '{0}' was found {1} times", find, count);
Console.ReadKey();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment