JulianJulianov

14.TextProcessingMoreExercise-Extract Person Information

Apr 23rd, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. 01. Extract Person Information
  2. Write a program that reads N lines of strings and extracts the name and age of a given person. The name of the person will be between '@' and '|'. The person’s age will be between '#' and '*'. Example: "Hello my name is @Peter| and I am #20* years old." For each found name and age print a line in the following format "{name} is {age} years old."
  3. Example
  4. Input                                        Output
  5. 2
  6. Here is a name @George| and an age #18*      George is 18 years old.
  7. Another name @Billy| #35* is his age         Billy is 35 years old.
  8.  
  9. 3
  10. random name @lilly| random digits #5* age    lilly is 5 years old.
  11. @Marry| with age #19*                        Marry is 19 years old.
  12. here Comes @Garry| he is #48* years old      Garry is 48 years old.
  13.  
  14. using System;
  15.                    
  16. public class Program
  17. {
  18.      public static void Main(string[] args)
  19.      {
  20.             var numbers = int.Parse(Console.ReadLine());
  21.             for (int i = 1; i <= numbers; i++)
  22.             {
  23.                 var nameAndAge = Console.ReadLine();         //Внимавай за вход като "abc@Pesho|abc", който е валиден! Виж най-долу!
  24.                 var name = "";
  25.                 var age = "";
  26.  
  27.                 var startIndexName = nameAndAge.IndexOf('@');
  28.                 var endIndexName = nameAndAge.IndexOf('|');
  29.                 name = nameAndAge.Substring(startIndexName + 1, endIndexName - startIndexName - 1);
  30.  
  31.                 var startIndexAge = nameAndAge.IndexOf('#');
  32.                 var endIndexAge = nameAndAge.IndexOf('*');
  33.                 age = nameAndAge.Substring(startIndexAge + 1, endIndexAge - startIndexAge - 1);
  34.  
  35.                 Console.WriteLine($"{name} is {age} years old.");
  36.             }
  37.      }
  38. }
  39.                                   //foreach (var item in nameAndAge)  //С този код нулевите тестове(от Example) са 100/100, но
  40.                                   //{                                 //всичко останало е 0/100, защото не се сещам, че входа
  41.                                       //if (item[0] == '@')  //може да е "Here is a name abc@George|def and an age a#18*b" например.
  42.                                       //{
  43.                                           //name = item.Substring(1, item.Length - 2);
  44.                                       //}
  45.                                       //else if (item[0] == '#')
  46.                                       //{
  47.                                           //years = item.Substring(1, item.Length - 2);
  48.                                       //}
  49.                                   //}
Advertisement
Add Comment
Please, Sign In to add comment