Answer to Question #15902 in C# for leena

Question #15902
write a prg that accepts 2 strings from the user. Also write code to print positions of all occurrences of the second string in the fisrt string using regular experssion.
1
Expert's answer
2012-10-05T07:58:28-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Occurrences_of_the_second_string_in_the_fisrt
{
class Program
{
static void Main(string[] args)
{
string firststring="";
string secondstring = "";
Console.Write("Enter first string: ");
firststring = Console.ReadLine();
Console.Write("Enter second string: ");
secondstring = Console.ReadLine();
Regex FindA = new Regex(secondstring);
int count = 0;
foreach (Match Tag in FindA.Matches(firststring))
{
Console.WriteLine("Found "+secondstring+" at {0}", Tag.Index);
count++;
if (count == 0) {
break;
}
}
if (count > 0) {
Console.WriteLine("Not found ");
}
Console.ReadLine();
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS