Advertisement
Guest User

Basic Methods & String Manipulation

a guest
Mar 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string name;    //Declaring variable "name" of string type
  14.             name = "";  //Initializing variable "name" to null
  15.             Console.WriteLine("Enter your full name plz."); //Print instruction for user to assign value to "name"
  16.             name = Console.ReadLine();  //Actually assign user input to "name"
  17.             DisplayFullName(name);  //Call DisplayFullName method sending value of "name"
  18.             Console.ReadKey();  //Halts the program until user inputs a key
  19.         }
  20.         public static string GetFirstName (string name) //Method called in DisplayFullName
  21.         {
  22.             int space;  //Declaring variable "space" of integer type
  23.             space = 0;  //Initializing variable "space" to null
  24.             space = name.IndexOf(" ");  //Assigning index of space character in string "name" to integer "space"
  25.             string fName;   //Declaring variable "fName" of string type
  26.             fName = ""; //Initializing variable "fName" to null
  27.             fName = name.Substring(0, space);   //Assigning value of "fName" to value of "name" from index of 0 to index represented by integer variable "space"
  28.             return fName;   //Returning value of "fName"
  29.         }
  30.         public static string GetLastName(string name) //Method called in DisplayFullName
  31.         {
  32.             int space;  //Declaring variable "space" of integer type
  33.             space = 0;  //Initializing variable "space" to null
  34.             space = name.IndexOf(" ");  //Assigning index of space character in string "name" to integer "space"
  35.             string lName;   //Declaring variable "lName" of string type
  36.             lName = ""; //Initializing variable "lName" to null
  37.             lName = name.Substring(space + 1);   //Assigning value of "lName" to value of "name" from index of 0 to index represented by integer variable "space" plus one to avoid including the space itself in "lName"
  38.             return lName;   //Returning value of "lName"
  39.         }
  40.         public static void DisplayFullName (string name)    //Method called in Main
  41.         {
  42.             Console.WriteLine("Your first name is {0} and your last name is {1}.",GetFirstName(name), GetLastName(name));   //Call necessary methods and output information to console
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement