Foundation Strengthening third day exercise summary

Source: Internet
Author: User

5, the number to capitalize case
#region Exercise: Convert 123 to: one and three. Dictionary<char,char>
Prompt for user input
Console.WriteLine ("Please enter a number");
Accept the user's input
String number = Console.ReadLine ();
String str = "1 One 2 II 3 three 4 restaurant 5 Wu Lu 6 seven 7 BA 8 JIU 9";
Dictionary<int, char> dic = new Dictionary<int, char> ();
Remove spaces from a string
string[] parts = str. Split (New char[]{'},stringsplitoptions.removeemptyentries);
Join a key-value pair in the collection
for (int i = 0; i < parts. Length; i++)
{
Dic. ADD (parts[i][0],parts[i][1]);
}
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < number. Length; i++)
{
Sb. Append (Dic[number[i]);
}
Console.WriteLine (sb.) ToString ());
Console.readkey ();
#endregion
#region the second method of
String str = "1 One 2 II 3 three 4 restaurant 5 Wu Lu 6 seven 7 BA 8 JIU 9";
Dictionary<char, char> dic = new Dictionary<char, char> ();
Remove spaces
string[] Number = str. Split (New []{'},stringsplitoptions.removeemptyentries);
Take these 10 items out of the array.
for (int i = 0; i < number. Length; i++)
{
Add these 10 items to the dictionary of key-value pairs
Because keys cannot be duplicated, so add a judgment
if (!dic. ContainsKey (Number[i][0]))
{
Dic. ADD (number[i][0],number[i][1]);
}
}
Prompt for user input
Console.WriteLine ("Please enter a number");
String Txt=console.readline ();
for (int i = 0; i < txt. Length; i++)
{
Determines whether the current dictionary contains user-entered numbers
if (DIC. ContainsKey (Txt[i]))
{
If you have this value, replace it with an uppercase Dic[txt[i]] This represents the value of the current key, the print output
Console.Write (Dic[txt[i]);
}
Else
{
Otherwise output as-is
Console.Write (Txt[i]);
}
}
Console.readkey ();
#endregion

#region Practice four counts the number of occurrences of each letter in the string (the interview question). "Welcome, to Chinaworld", Case insensitive, print "W2" "E 2" "O 3" ...
String str = "Welcome, to Chinaworld";
str = str. ToLower ();
Dictionary<char, int> dic = new Dictionary<char, int> ();
for (int i = 0; i < str. Length; i++)
{
if (char. Isletter (Str[i]))
{
if (!dic. ContainsKey (Str[i])//If the dictionary does not contain this character
{
This character is added, and the number of occurrences is 1 times
Dic. ADD (Str[i], 1);
}
Else
{
The value of the key corresponds to the number of times plus 1
dic[str[i]]++;
}
}
}
foreach (keyvaluepair<char,int> item in DIC)
{
Console.WriteLine ("The letter {0} appears {1} times", item.) Key,item. Value);
}
Console.readkey ();
#endregion


6. File operation
Write file
using (FileStream fs=new FileStream ("5.txt", Filemode.openorcreate,fileaccess.write))
//{
String str = "The weather is fine today";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes (str);
Fs. Write (Buffer,0,buffer. Length);
//}
Console.WriteLine ("OK");
Console.readkey ();
Read file
using (FileStream fs=new FileStream ("5.txt", Filemode.openorcreate,fileaccess.read))
{
Byte[] Buffer=new Byte[fs. Length];
Fs. Read (Buffer,0,buffer. Length);
String msg = System.Text.Encoding.UTF8.GetString (buffer);
Console.WriteLine (msg);
}
Console.readkey ();

7. Double the Salary
#region Exercise 3: Work with payroll files through StreamReader and Streamwrite
Double the salary in a file and write it to a file
using (StreamReader sr=new StreamReader ("Salary.txt", Encoding.default))
{
using (StreamWriter sw=new StreamWriter ("A.txt", False,encoding.default))
{
String line;
while ((LINE=SR. ReadLine ())!=null)
{
string[] msg = line. Split (New char[]{' | '},stringsplitoptions.removeemptyentries);
String str = string. Format ("{0}|{ 1} ", Msg[0],convert.toint32 (msg[1));
Sw. Write (str);
}
}
}
Console.WriteLine ("OK");
Console.readkey ();
#endregion

8, Path class
           //1.
           //string lujing = @ "C:\1.txt";
           ////is primarily the suffix name
            //string msg = path.changeextension (lujing, ". rar");
           //console.writeline (msg);
           //console.readkey ();

           //2. Merge Paths
            //string str1 = @ "C:\Program Files (x86) \";
           //string str2 = @ "Microsoft SQL server\100\ Sdk\assemblies ";
           //string msg = Path.Combine (STR1,STR2);
           //console.writeline (msg);
           //console.readkey ();

3. Find the path where a file resides
String str = @ "C:\Program Files (x86) \microsoft SQL Server\100\sdk\assemblies";
String msg = Path.getdirectoryname (str);
Console.WriteLine (str);
Console.readkey ();

Path.getextension ();//return extension
Path.getfilename ();//Get file name
Path.GetFullPath ();//returns the absolute path of the specified string

Foundation Strengthening third day exercise summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.