C# Language Fundamentals Hands-On-Lab-Guide - (Questions and Answers)
C# Language Fundamentals Hands-On-Lab-Guide - (Questions and Answers)
Note: Objective of this exercise is come out with a different solution based on the answer for each
question.
Solution:
using System;
using System.Collections.Generic;
1. Write a program to accept a string and display the longest word in the given
string.
For Example: It is a new generation of world
Now display longest word which is generation
using System;
public class Ex4
{
public static void Main()
{
string line = " It is a new generation of world ”;
string[] words = line.Split();
string word = "";
int ctr = 0;
foreach (String s in words)
{
if (s.Length > ctr)
{
word = s;
ctr = s.Length;
}
}
Console.WriteLine(word);
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System;
using System.Collections.Generic;
Solution : Note: Code is given as clue for the answer try modifying
the code to get the desired output.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
}
}
10.) Write a C# Sharp program to get the day of the week for a
specified date.
using System;
using System;
public class Example15
{
public static void Main()
{
DateTime baseDate = new DateTime(2016, 2, 29);
Console.WriteLine(" Base Date: {0:d}\n", baseDate);
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
0
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
}
}
using System;
public class Ex13
{
public static void Main()
{
DateTime august14 = new DateTime(2009, 8, 14, 5, 23, 15);
IFormatProvider culture =
new System.Globalization.CultureInfo("ja-JP", true);
// Get the short date formats using the "ja-JP" culture.
string [] frenchmay12Formats =
may12.GetDateTimeFormats(culture);
class Ex17
{
static void Main()
{
DateTime localDateTime, univDateTime;
try {
localDateTime = DateTime.Parse(strDateTime);
univDateTime = localDateTime.ToUniversalTime();
try {
univDateTime = DateTime.Parse(strDateTime);
localDateTime = univDateTime.ToLocalTime();
}
}
using System;
class dttimeex18
{
static void Main()
{
int yr, mn, dt;
class dttimeex19
{
static void Main()
{
Console.Write("\n\n Compute what day was Yesterday :\n");
Console.Write("--------------------------------------\n");
Console.WriteLine(" Today is : {0}",
DateTime.Today.ToString("dd/MM/yyyy"));
DateTime yd = GetYesterday();
Console.WriteLine(" The Yesterday was : {0} \n",
yd.ToString("dd/MM/yyyy"));
}
static DateTime GetYesterday()
{
return DateTime.Today.AddDays(-1);
}
}
class dttimeex20
{
static void Main()
{
Console.Write("\n\n Compute what day will be Tomorrow :\n");
Console.Write("----------------------------------------\n");
Console.WriteLine(" Today is : {0}",
DateTime.Today.ToString("dd/MM/yyyy"));
DateTime dt = GetTomorrow();
Console.WriteLine(" The Tomorrow will be : {0} \n",
dt.ToString("dd/MM/yyyy"));
}
static DateTime GetTomorrow()
{
return DateTime.Today.AddDays(1);
}
}
class dttimeex22
{
static void Main()
{
int mn,yr;
}
}
22.) Write a program in C# Sharp to create a file and move the
file into the same directory to with different name.
using System;
using System.IO;
using System.Text;
if (File.Exists(sfileName))
{
File.Delete(sfileName);
}
if (File.Exists(tfileName))
{
File.Delete(tfileName);
}
Console.Write("\n\n Create a file and move the file in
same folder to another name :\n");
Console.Write("------------------------------------------
----------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(sfileName))
{
fileStr.WriteLine(" Hello and Welcome");
fileStr.WriteLine(" It is the first content");
fileStr.WriteLine(" of the text file mytest.txt");
}
using (StreamReader sr = File.OpenText(sfileName))
{
string s = "";
Console.WriteLine(" Here is the content of the
file {0} : ",sfileName);
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
System.IO.File.Move(sfileName, tfileName); // move a file to another name
in same location:
Console.WriteLine(" The file {0} successfully moved to the name {1} in the
same directory.",sfileName,tfileName );
class filexercise22
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i;
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0;i<n;i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
Console.Write("\n The content of the last line of the file {0} is :\
n",fileName);
if (File.Exists(fileName))
{
string[] lines = File.ReadAllLines(fileName);
Console.WriteLine(" {0}",lines[n-1]);
}
Console.WriteLine();
}
}
using System;
using System.IO;
class filexercise24
{
static void Main()
{
string fileName = @"mytest.txt";
string[] ArrLines ;
int n,i,l;
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write(" Input number of lines to write in the file :");
n= Convert.ToInt32(Console.ReadLine());
ArrLines=new string[n];
Console.Write(" Input {0} strings below :\n",n);
for(i=0;i<n;i++)
{
Console.Write(" Input line {0} : ",i+1);
ArrLines[i] = Console.ReadLine();
}
System.IO.File.WriteAllLines(fileName, ArrLines);
Console.WriteLine();
}
}
class filexercise25
{
public static void Main()
{
string fileName = @"mytest.txt";
int count;
try
{
// Delete the file if it exists.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write("\n\n Count the number of lines in a file :\n");
Console.Write("------------------------------------------\
n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(fileName))
{
fileStr.WriteLine(" test line 1");
fileStr.WriteLine(" test line 2");
fileStr.WriteLine(" Test line 3");
fileStr.WriteLine(" test line 4");
fileStr.WriteLine(" test line 5");
fileStr.WriteLine(" Test line 6");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
count=0;
Console.WriteLine(" Here is the content of the
file mytest.txt : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
count++;
}
Console.WriteLine("");
}
Console.Write(" The number of lines in the file {0} is : {1} \n\
n",fileName,count);
}
catch (Exception MyExcep)
{
Console.WriteLine(MyExcep.ToString());
}
}
}