w3resource

C#: Indicate whether each string in an array ends with a period (".")

C# Sharp String: Exercise-41 with Solution

Write a C# Sharp program to indicate whether each string in an array ends with a period (".").

C# Sharp Exercises: Indicate whether each string in an array ends with a period (".").

Sample Solution:-

C# Sharp Code:

using System;

public class Example41
{
   public static void Main()
   {
      // Declare an array of strings
      String[] strings = { "Actions speak louder than words", "Hello!", "Python.", 
                           "PHP.", "random" };

      // Iterate through each string in the array
      foreach (var value in strings) {
         // Check if the string ends with a period
         bool endsInPeriod = value.EndsWith(".");
         // Print whether the string ends with a period or not
         Console.WriteLine();
         Console.WriteLine("'{0}' ends in a period: {1}", 
                           value, endsInPeriod);
      }                            
   }
}

Sample Output:

'Actions speak louder than words' ends in a period: False                                                     
                                                                                                              
'Hello!' ends in a period: False                                                                              
                                                                                                              
'Python.' ends in a period: True                                                                              
                                                                                                              
'PHP.' ends in a period: True                                                                                 
                                                                                                              
'random' ends in a period: False

Flowchart :

Flowchart: C# Sharp Exercises - Indicate whether each string in an array ends with a period (".").

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to demonstrates the CopyTo method.
Next: Write C# Sharp program to check whether a string occurs at the end of another string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/string/csharp-string-exercise-41.php