0% found this document useful (0 votes)
49 views

Lab2 - Strings

This document contains 4 programs written in C# to perform string operations. Program 1 concatenates and inverts a first and last name. Program 2 prints the last 4 characters of a national ID. Program 3 finds the index of "c#" in a string and prints the new string length after removing empty characters. Program 4 lists all substrings of a given string.

Uploaded by

ezz Ayman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Lab2 - Strings

This document contains 4 programs written in C# to perform string operations. Program 1 concatenates and inverts a first and last name. Program 2 prints the last 4 characters of a national ID. Program 3 finds the index of "c#" in a string and prints the new string length after removing empty characters. Program 4 lists all substrings of a given string.

Uploaded by

ezz Ayman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1st Level | struct. Prog.

| Lab 2
Lab#2:Strings

Program#1: Write a C# program that concatenates Your Firstname and Lastname and
then prints your fullName inverted.

using System;
namespace invertedName
{
class Program
{
static void Main(string[] args)
{
string firstname, lastname;
Console.WriteLine("Plz Enter your FirstName: ");
firstname = Console.ReadLine();
Console.WriteLine("Plz Enter your LastName: ");
lastname = Console.ReadLine();
string Name = string.Concat(firstname," ", lastname);
Console.WriteLine(Name);
Console.WriteLine("***Inverted Name**** : ");
for (int i = Name.Length - 1; i >= 0; i--)
{
Console.WriteLine(Name[i]);
}

}
}
}

Output:

Page | 1
1st Level | struct. Prog. | Lab 2

Program#2: Write a C# Code that accepts your National ID and then


prints the last four numbers.
using System;

namespace Mtable
{
class Program
{
static void Main(string[] args)
{
string NationalId;
Console.WriteLine("Plz Enter Your National ID: ");
NationalId = Console.ReadLine();
string Last4nums = NationalId.Substring(10, 4);
Console.WriteLine("The last 4 nums are: {0}", Last4nums);
}
}
}

Output:

Page | 2
1st Level | struct. Prog. | Lab 2
Program#3: Write a C# code that produces the number of index of c# in the string “
Welcome to c# programming Language. ” , removes all its empty characters and finally
prints the size of the new string.

using System;

namespace index
{
class Program
{
static void Main(string[] args)
{
string str = "Welcome to c# programming Language. ";
int index = str.IndexOf("c#");
string str1=string.Format("index of c# in \"Welcome to c# programming
Language. \" is : \n {0} ",index);
Console.WriteLine(str1);
string emptychar= str.Replace(" ", "");
Console.WriteLine("My String without empty charactr : \n {0} ", emptychar);
Console.WriteLine("the size of empty string is : \n {0} ",
emptychar.Length);

}
}
}

output:

Page | 3
1st Level | struct. Prog. | Lab 2
Program#4: Write a C# program to list all substrings in agiven string.
using System;

namespace substrings
{
class Program
{
static void Main(string[] args)
{
string substring;
string[] a = new string[5];
Console.WriteLine("Enter the String : ");
string value = Console.ReadLine();
Console.WriteLine("All Possible Substrings of the Given String are :");
for (int i = 1; i <= value.Length; i++)
{
for (int j = 0; j <= value.Length - i; j++)
{
substring = value.Substring(j, i);
a[j] = substring;
Console.WriteLine(a[j]);
}

}
}
}
}

Output:

Page | 4

You might also like