12.3 P3 - Ex1: Substrings Compute and output the number of ways in which we can
ID: 3698501 • Letter: 1
Question
12.3 P3 - Ex1: Substrings Compute and output the number of ways in which we can choose two identical non-overlapping substrings of a given string read from the user Constraints Read the string from the user. The given string will consist only of lowercase English letters (a-z). The length of the given string will be between 1 and 50, inclusive. . . Output your result using the following format'%d ". Example 1 When the input is Output must be Example 2 When the input is aba Output must be Example 3 When the input is abab Output must be LAB ACTIVITY 12.3.1: P3- Ex1: Substrings 0/65Explanation / Answer
ANS::>
Hi, the answer code is given below. I have ran the code with above example. It's giving successful outputs.
Please copy the code in your IDE, Compile it and run it with above examples.
using System;
public class Program
{
public static void Main()
{
string n = Console.ReadLine();
int count=0;
for(int i=0;i<n.Length;i++) //1st for loop for getting the number of counts for single character
{
for(int j=i+1;j<n.Length;j++)
{
if(n[i]==n[j])
{
count++;
}
}
}
//2nd forloop for getting the count with length more than 1 eg: ab,ab or an,an
for(int i =2;i<=n.Length/2;i++)
{
for(int j=0;j<=n.Length/2;j++)
{
for(int x=j+i;x<=n.Length/2;x++)
{
if(n.Substring(j,i)==n.Substring(x,i))
{
count++;
}
}
}
}
Console.WriteLine(count);
}
}
In case of any problem write in the comment section , I will be happy to help :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.