Tutorial 3: Ans: S Hello S1 He S2 Hel S3 Llo S4 LL
Tutorial 3: Ans: S Hello S1 He S2 Hel S3 Llo S4 LL
i) Using system.string;
Using system;
Class str1
{
public static void Main()
{
string s1=”computer”;
string s2=”science”;
string s3=string.concat(s1,s2);
Console.WriteLine(“s1:”+s1);
Console.WriteLine(“s2:”+s2);
Console.WriteLine(“s3:”+s3);
}
}
Ans:
S1:computer
S2:science
S3:computer science
3. Write a program that reads a list of names in random order and display them in
alphabetic order using i) compare() method 2) equals() method.
/* compare method*/
Using system;
Using system.string;
Class alpha
{
public static void Main()
{
string temp;
string[]names={“Kabir”,”barath”,”dinesh”,Jeni”};
int n=names.Length;
for(int j=i+1;j<n;j++)
{
if(string.compare(names[i],names[j])==0)
{
Console.WriteLine(“They are equal”);
}
if(string.compare(names[i],names[j])==1)
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
Console.WriteLine(“names sorted:”+names[i]);
}
}
Ans:
Names sorted:
Barath
Dinesh
Jeni
Kabir
/* Equals method*/
Using system;
Using system.string;
Class sort
{
public static void Main()
{
string temp;
string[]names={“Kabir”,”barath”,”dinesh”,Jeni”};
int n=names.Length;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
bool b=string.Equals(names[i],names[j]);
if(b==true)
{
Console.WriteLine(“They are equal”);
}
else
{
temp=names[i];
names[i]=names[j];
names[j]=temp;
}
}
Console.WriteLine(“names sorted:”+names[i]);
}
}
}
Ans:
Names sorted:
Barath
Dinesh
Jeni
Kabir
6.Write a program that counts the number of occurrence a particular character in a line
text.
Using system;
Using system.string;
Class count
{
public static void Maic()
{
string s1=Console.ReadLine();
int n=s1.Length;
int count;
for(int i=0;i<n;i++)
{
if(s[i]==’e’)
{
count++;
}
Console.WriteLine(“Number of occurrences” +count);
}
}
}
Ans:
Committee
Number of occurrences:2
Using system;
Using system.string;
Class count
{
public static void Main()
{
string [ ] files =new string[20];
int count=0;
int i=0;
while( files[i]!=’\o’)
{
count++;
i++;
}
Console.WriteLine(“number of words” +count);
}
}
8.Write a program that reads a line of text containing three words and then replaces all
the blank spaces with a underscore,
using system;
using system.Text;
using system.String;
class score
{
public static void Main()
{
string s1;
string s2;
Console.WriteLine(“Enter three words”);
S1=Console.ReadLine();
S2=s1.Replace(‘\o’,’_’);
Console.WriteLine(“String now”+s2);
}
}
Ans:
Enter three words: I AM INDIAN
String Now :I_AM_INDIAN