Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 01. Extract Person Information
- 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."
- Example
- Input Output
- 2
- Here is a name @George| and an age #18* George is 18 years old.
- Another name @Billy| #35* is his age Billy is 35 years old.
- 3
- random name @lilly| random digits #5* age lilly is 5 years old.
- @Marry| with age #19* Marry is 19 years old.
- here Comes @Garry| he is #48* years old Garry is 48 years old.
- using System;
- public class Program
- {
- public static void Main(string[] args)
- {
- var numbers = int.Parse(Console.ReadLine());
- for (int i = 1; i <= numbers; i++)
- {
- var nameAndAge = Console.ReadLine(); //Внимавай за вход като "abc@Pesho|abc", който е валиден! Виж най-долу!
- var name = "";
- var age = "";
- var startIndexName = nameAndAge.IndexOf('@');
- var endIndexName = nameAndAge.IndexOf('|');
- name = nameAndAge.Substring(startIndexName + 1, endIndexName - startIndexName - 1);
- var startIndexAge = nameAndAge.IndexOf('#');
- var endIndexAge = nameAndAge.IndexOf('*');
- age = nameAndAge.Substring(startIndexAge + 1, endIndexAge - startIndexAge - 1);
- Console.WriteLine($"{name} is {age} years old.");
- }
- }
- }
- //foreach (var item in nameAndAge) //С този код нулевите тестове(от Example) са 100/100, но
- //{ //всичко останало е 0/100, защото не се сещам, че входа
- //if (item[0] == '@') //може да е "Here is a name abc@George|def and an age a#18*b" например.
- //{
- //name = item.Substring(1, item.Length - 2);
- //}
- //else if (item[0] == '#')
- //{
- //years = item.Substring(1, item.Length - 2);
- //}
- //}
Advertisement
Add Comment
Please, Sign In to add comment