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
string temp; string[]names={“Kabir”,”barath”,”dinesh”,Jeni”}; int n=names.Length; for(int i=0;i
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
} 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 Predefined reference typr String is an object of for representing sequence of system. String class since characters in C# all datatypes are considered objects in C# Eg:”abc” \\String Eg: string s1; \\declaring a string object String s1=”abc” \\ string declaring and assigning
String literals String literal is used to assign a quoted string of characters to a string object Eg: s1=”abc” \\ assigning string literal
10 Difference between mutable and immutable strings. Mutable string Mutable strings are modifiable strings Supported by string builder class Eg:Append() Appendformat()
Immutable string Immutable strings are not modifiable strings Supported by system.string class Eg :copy() Concat()