Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MethodsOfTranslations
- {
- using System;
- public class Program
- {
- // Sample Input and Output
- //
- // |----------|----------------------------------------------------|
- // | Input | Output |
- // |----------|----------------------------------------------------|
- // | |Type a word to check whether it is in the language: |
- // |(null) | Word is in the language. |
- // | 0 | Word is in the language. |
- // | 0011 | Word is in the language. |
- // | 1100 | Word is not in the language. |
- // | asdasd | Word is not in the language. |
- // | End | The program has ended. |
- // |----------|----------------------------------------------------|
- public static void Main(string[] args)
- {
- Console.WriteLine("Type a word to check whether it is in the language:");
- string word = Console.ReadLine();
- while (word != "End") // Program stops upon "End" input
- {
- // Conditional statement checks whether word is part of the language
- if (IsWordInFormalLanguage(word))
- Console.WriteLine("Word is in the language.");
- else
- Console.WriteLine("Word is not in the language.");
- word = Console.ReadLine(); // The user needs to enter a new word, therefore repeating the loop.
- }
- Console.WriteLine("The program has ended.");
- }
- public static bool IsWordInFormalLanguage(string word)
- {
- // Checks if the word is null, "1", or "0".
- if(string.IsNullOrEmpty(word) || word == "1" || word == "0")
- {
- // If it's true there is no need to proceed.
- return true;
- }
- // Iterates input word
- for (int i = 0; i < word.Length; i++)
- {
- // Checks if the input contains only "1s", and "0s".
- if (word[i] != '1' && word[i] != '0')
- {
- // The word is a part of the language.
- return false;
- }
- // Checks whether there is another letter after the current word[i]
- if (i != word.Length-1)
- {
- // Checks whether the letter is "1"
- if (word[i] == '1')
- {
- // Checks whether the next letter is "0".
- if (word[i + 1] == '0')
- {
- // If the next letter is "0", the program will return false because
- // from the problem description, we know that this is not in the language scope.
- // The word is not a part of the language.
- return false;
- }
- }
- }
- }
- // The word is a part of the language.
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement