Wednesday, May 13, 2009

Lab 13 Study

Lab 13 Be sure to put each class in its own file. The referenced pages contain four short programs that demonstrate string functions. Combine logic from these programs to create your own big program that demonstrates these string functions. Put all this in a loop. Accept one or more strings from the user and use these strings in the string functions in your program.

// Fig. 16.5: StringIndexMethods.cs
// Using string searching methods.
using System;

class StringIndexMethods
{
public static void Main()
{

string string1, string2, string3, inputkey;
inputkey = "n";
while (inputkey != "q" && inputkey != "Q")
{

Console.WriteLine("\nPlease Enter the first string");
string1=Convert.ToString(Console.ReadLine());
Console.WriteLine("\nPlease Enter the second string");
string2=Convert.ToString(Console.ReadLine());
string3=string.Concat( string1, string2 );

Console.WriteLine("The Concat of the String1 and String2 is {0}", string3);
Console.WriteLine( "\nstring1.ToUpper() = \"" +
string1.ToUpper() + "\"\nstring2.ToLower() = \"" +
string2.ToLower() + "\"" );

// call Trim method
Console.WriteLine( "\nstring3 after trim = \"" +
string3.Trim() + "\"" );

if (string1.Contains("a")==true)
{
Console.WriteLine(
"\nReplacing \"a\" with \"A\" in string1: \"" +
string1.Replace( 'a', 'A' ) + "\"" );
}
Console.WriteLine("\nThe length of the String 1 is " + string1.Length);
Console.WriteLine("\nThe length of the String 2 is " + string2.Length);
Console.WriteLine("\nThe length of the String 3 is " + string3.Length);
Console.WriteLine( "\nstring1 = \"" + string1 + "\"" );

Console.WriteLine("Press Enter to continue, Press q to quit!");
inputkey = Convert.ToString(Console.ReadLine());

Console.WriteLine("");

} //while loop






} // end method Main
} // end class StringIndexMethods

No comments:

Post a Comment