Advertisement
mdgaziur001

colonbracket lang first prototype

Sep 28th, 2021
1,188
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct UppercaseString {
  2.     pub data: String, // public field
  3.     _unused_private_field: u32 // private field
  4. }
  5.  
  6. impl UppercaseString {
  7.     pub func new(str: &String) -> Self {
  8.         let copy = String::empty();
  9.         for ch in str {
  10.             copy += ch.tolower();
  11.         }
  12.  
  13.         UppercaseString {
  14.             data: copy,
  15.             _unused_private_field: 0
  16.         }
  17.     }
  18.  
  19.     pub func print(&self) {
  20.         io::print(f"UppercaseString {{ data: {self.data} }}");
  21.     }
  22. }
  23.  
  24. func first_and_last_equal(source: &String) -> bool {
  25.     source[0] == source[source.len()]
  26. }
  27.  
  28. func main() {
  29.     let input_string = io::input<String>(None);
  30.  
  31.     // make all letters lowercase(case insensitive)
  32.     for idx of input_string {
  33.         input_string[idx] = input_string[idx].to_lower();
  34.     }
  35.  
  36.     // now make a copy and reverse
  37.     let another_string = input_string.copy();
  38.     another_string.reverse();
  39.  
  40.     if input_string == another_string {
  41.         io::println("Palindrome");
  42.     } else {
  43.         io::println("Not Palindrome");
  44.     }
  45.  
  46.     // check if first and last letter is equal using a function call
  47.     if first_and_last_equal(&input_string) {
  48.         io::println("First and last character equal");
  49.     }
  50.  
  51.     // instantiate UppercaseStruct and call print of that method
  52.     UppercaseStruct::new(input_string).print();
  53. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement