Вы находитесь на странице: 1из 11

1. WAP to check whether the string entered is palindrome or not.

using System;
namespace palindrome
{
class Program
{
static void Main(string[] args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome \n Entered String Was {0} and
reverse string is {1}", s, revs);
}
else
{
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and
reverse string is {1}", s, revs);
}
Console.ReadKey();
}
}
}
2. WAP to demonstrate the concepts of properties and indexers.

using System;

namespace IndexerApp
{

class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;

public IndexedNames()
{
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index]
{
get
{
string tmp;

if (index >= 0 && index <= size - 1)


{
tmp = namelist[index];
}
else
{
tmp = "";
}

return (tmp);
}
set
{
if (index >= 0 && index <= size - 1)
{
namelist[index] = value;
}
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Himanshu";
names[1] = "Kumar";
names[2] = "Vibhav";
names[3] = "Rathore";
names[4] = "Hardik";

for (int i = 0; i < IndexedNames.size; i++)


{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}

3. WAP of generic List.

using System;
using System.Collections.Generic;

namespace Genericlist
{
public class Program
{
public static void Main(string[] args)
{
//Adding Item
List<string> myList = new List<string>();
myList.Add("Himanshu");
myList.Add("Abhishek");
myList.Add("kamal");

//Printing Item
foreach (string s in myList)
Console.Write(s.ToString() + " ");

//Sorting List
myList.Sort();
Console.WriteLine("\n After Sorting");
foreach (string s in myList)
Console.Write(s.ToString() + " ");

//Removing Items
myList.Remove("Abhishek");
Console.WriteLine("\nRemoving Abhishek");
foreach (string s in myList)
Console.Write(s.ToString() + " ");

//Inserting Item in the middle


myList.Insert(2, "Vaibhav");
Console.WriteLine("\nInserting vaibhav at index position 2");
foreach (string s in myList)
Console.Write(s.ToString() + " ");
Console.WriteLine(" ");
}
}
}

4. Write a program to input a string through keyboard and display the number
of vowels.

using System;

namespace vowels
{
public class vowel
{
public static void Main()
{
string str;
int i, len, vowel;

Console.Write("\n\nCount total number of vowel \n");


Console.Write("----------------------------------------------\n");
Console.Write("Input the string : ");
str = Console.ReadLine();

vowel = 0;
len = str.Length;

for (i = 0; i < len; i++)


{

if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||


str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||
str[i] == 'U')
{
vowel++;
}
}
Console.Write("\nThe total number of vowel in the string is : {0}\n", vowel);
}
}
}

5. Write a program to input an alphabet character in lower case and display in


an upper case or vice versa.
using System;

public class lowerupper


{
public static void Main()
{
string str1;
char[] arr1;
int l, i;
l = 0;
char ch;
Console.Write("\n\nReplace lowercase characters by uppercase and vice-versa
:\n");
Console.Write("--------------------------------------------------------------
\n");
Console.Write("Input the string : ");
str1 = Console.ReadLine();
l = str1.Length;
arr1 = str1.ToCharArray(0, l); // Converts string into char array.

Console.Write("\nAfter conversion, the string is : ");


for (i = 0; i < l; i++)
{
ch = arr1[i];
if (Char.IsLower(ch)) // check whether the character is lowercase
Console.Write(Char.ToUpper(ch)); // Converts lowercase character to
uppercase.
else
Console.Write(Char.ToLower(ch)); // Converts uppercase character to
lowercase.
}
Console.Write("\n\n");
}
}

6. Display date and time using message box in window forms.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace Date_and_Time

public partial class Form1 : Form

public Form1()
{

InitializeComponent();

private void Form1_Load(object sender, EventArgs e)

timer1.Start();

label1.Text = DateTime.Now.ToLongTimeString();

label2.Text = DateTime.Now.ToLongDateString();

private void timer1_Tick(object sender, EventArgs e)

label1.Text = DateTime.Now.ToLongTimeString();

timer1.Start();

7. Write a recursive method that displays the contents of a string backwards

using System;

class ReverseString
{

static void reverse(String str)


{
if ((str == null) || (str.Length <= 1))
Console.Write(str);

else
{
Console.Write(str[str.Length - 1]);
reverse(str.Substring(0, (str.Length - 1)));
}
}

public static void Main()


{
String str;
Console.WriteLine("Enter the String");
str = Console.ReadLine();
reverse(str);

}
}

8. Create a stack class called stack that can hold characters. Call the methods
that access the stack Push() and POP(). Allow the user to specify the size of
the stack when it is created.

using System;

class Stack

static int []stk;

static int top=-1,size;

static void push(){

int num;
if(top==size-1)

Console.WriteLine("Stack if full.");

else{

Console.Write("Enter element : ");

num=int.Parse(Console.ReadLine());

top+=1;

stk[top]=num;

static void pop(){

if(top==-1)

Console.WriteLine("Stack if empty.");

else{

Console.WriteLine("Element poped is "+stk[top]);

top-=1;

static void display(){

Console.WriteLine("Stack is : ");

for(int i=top;i>=0;i--){

Console.WriteLine(stk[i]);

public static void Main(){

Console.Write("Enter Stack size : ");


size=int.Parse(Console.ReadLine());

stk=new int[size];

int ch=0;

while(ch!=4){

Console.WriteLine("Enter choice : \n1.Push\n2.Pop\n3.Display\n4.Exit");

ch=int.Parse(Console.ReadLine());

switch(ch){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

Вам также может понравиться