Você está na página 1de 6

Tutorial 3

1. Write a C# program to display the following statements using the


string “HELLO”& method substring.Display Hello,He,,Hel,llo,ll.
Using system;
Using system.string;
Class message
{
public static void Main()
{
string s=”Hello”;
string s1=s.substring(0,1);
string s2=s.substring(0,2);
string s3=s.substring(2);
string s4=s.substring(2,3);
Console.WriteLine(“S:”+s);
Console.WriteLine(“S1:”+s1);
Console.WriteLine(“S2:”+s2);
Console.WriteLine(“S3:”+s3);
Console.WriteLine(“S4:”+s4);
}
}
Ans:
S=hello
S1=He
S2=Hel
S3=llo
S4=ll
2. Write a program that reads the name “computer science”as two
separate strings and concatenate them into new string i) = operator ii)
Append method.
Using system.string;
Using system;
Class str
{
public static void Main()
{
string s1=”computer”;
string s2=”science”;
string s3=s1+s2;
Console.WriteLine(“s1:”+s1);
Console.WriteLine(“s2:”+s2);
Console.WriteLine(“s3:”+s3);
}
}
Ans:
S1:computer
S2:science
S3:computer science

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

ii) Using system.string;


Using system;
Class str2
{
public static void Main()
{
stringBuilder s=new stringbuilder(“computer”);
s.Append(“science”);
Console.WriteLine(Äppended string :”+s););
}
}
Ans:
Appended string: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

4.Point out errors if any in the following statements


a) string s1=(“abc”+”ABC”);
Ans: Error string s1=”abc”+”ABC”
b)string s2=s1+”abc”
Ans:No error
c)String s1.copy(“abc”
Ans:Error string s1=string.copy(“abc”)
d) string s3=”@\ABC\n\c\sharp”
Ans: string s3=@”\ABC\n\c\sharp”;

5.state whether the following statement would run.


a) int n=s1.length;
Ans:Returns length of string s,to integer valuen.
b) int m=s1.indexof(“are”);
Ans:Returns position of substring in a string.
c) string s2=s1.insert(s1.indexof(“or”),not”);
Ans:inserts the string at position of substring in a string
d) int n=string.compare(s1,s2,false);
Ans:Compare two strings s1 and s2
e)Console.WriteLine(s1,[5]);
Ans:Writes the character and specified position.

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

7.Write a program that counts the number of words in a text document.

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

9.Difference between strings,objects and string literals.


Strings String objects String literals
Predefined reference typr String is an object of String literal is used to
for representing sequence of system. String class since assign a quoted string of
characters in C# all datatypes are considered characters to a string object
objects in C#
Eg:”abc” \\String Eg: string s1; \\declaring a Eg: s1=”abc” \\ assigning
string object string literal
String s1=”abc” \\ string declaring and assigning

10 Difference between mutable and immutable strings.

Mutable string Immutable string


Mutable strings are modifiable strings Immutable strings are not modifiable
strings
Supported by string builder class Supported by system.string class
Eg:Append() Eg :copy()
Appendformat() Concat()

Você também pode gostar