Advertisement
alexpeevk9

Translation Methods - Part 1

May 1st, 2022 (edited)
1,440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. namespace MethodsOfTranslations
  2. {
  3.     using System;
  4.     public class Program
  5.     {
  6.         // Sample Input and Output
  7.         //
  8.         // |----------|----------------------------------------------------|
  9.         // | Input    | Output                                             |
  10.         // |----------|----------------------------------------------------|
  11.         // |          |Type a word to check whether it is in the language: |
  12.         // |(null)    | Word is in the language.                           |
  13.         // | 0        | Word is in the language.                           |
  14.         // | 0011     | Word is in the language.                           |
  15.         // | 1100     | Word is not in the language.                       |
  16.         // | asdasd   | Word is not in the language.                       |
  17.         // | End      | The program has ended.                             |
  18.         // |----------|----------------------------------------------------|
  19.  
  20.         public static void Main(string[] args)
  21.         {
  22.             Console.WriteLine("Type a word to check whether it is in the language:");
  23.             string word = Console.ReadLine();
  24.             while (word != "End") // Program stops upon "End" input
  25.             {
  26.                 // Conditional statement checks whether word is part of the language
  27.                 if (IsWordInFormalLanguage(word))
  28.                     Console.WriteLine("Word is in the language.");
  29.                 else
  30.                     Console.WriteLine("Word is not in the language.");
  31.  
  32.                 word = Console.ReadLine(); // The user needs to enter a new word, therefore repeating the loop.
  33.             }
  34.             Console.WriteLine("The program has ended.");
  35.         }
  36.  
  37.         public static bool IsWordInFormalLanguage(string word)
  38.         {
  39.  
  40.             // Checks if the word is null, "1", or "0".
  41.             if(string.IsNullOrEmpty(word) || word == "1" || word == "0")
  42.             {
  43.                 // If it's true there is no need to proceed.
  44.                 return true;
  45.             }
  46.            
  47.             // Iterates input word
  48.             for (int i = 0; i < word.Length; i++)
  49.             {
  50.                 // Checks if the input contains only "1s", and "0s".
  51.                 if (word[i] != '1' && word[i] != '0')
  52.                 {
  53.                     // The word is a part of the language.
  54.                     return false;
  55.                 }
  56.  
  57.                 // Checks whether there is another letter after the current word[i]
  58.                 if (i != word.Length-1)
  59.                 {
  60.                     // Checks whether the letter is "1"
  61.                     if (word[i] == '1')
  62.                     {
  63.                         // Checks whether the next letter is "0".
  64.                         if (word[i + 1] == '0')
  65.                         {
  66.                             // If the next letter is "0", the program will return false because
  67.                             // from the problem description, we know that this is not in the language scope.
  68.                             // The word is not a part of the language.
  69.                             return false;
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.             // The word is a part of the language.
  75.             return true;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement