﻿ C# - Check if the string birds is a substring of a familiar

# C#: Determine whether the string "birds" is a substring of a familiar

## C# Sharp String: Exercise-38 with Solution

Write a C# Sharp program to determine whether the string "birds" is a substring of a familiar string.

Note : Quotation ‘two birds with one stone'.

Sample Solution:-

C# Sharp Code:

``````using System;

class Example38
{
public static void Main()
{
string str1 = "Kill two birds with one stone"; // Original string
string str2 = "birds"; // Substring to be checked

// Check if str1 contains str2 using str1.Contains()
bool x = str1.Contains(str2);
Console.WriteLine("'{0}' is in the string '{1}': {2}",
str2, str1, x);

// If str2 is found in str1
if (x) {
// Get the index of the first occurrence of str2 in str1 using str1.IndexOf()
int index = str1.IndexOf(str2);

// Display the index where str2 begins in str1
if (index >= 0)
Console.WriteLine("'{0}' begins at character position {1}",
}
}
}
```
```

Sample Output:

```'birds' is in the string 'Kill two birds with one stone': True
'birds begins at character position 10
```

Flowchart :

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿