C# training day 1
Par Nicolas, lundi 6 juin 2011 à 16:15 :: geek stuff :: #493 :: rss
I'm getting serious about C# and had a course today on the basics :
- C# overview
- Visual Studio
- Projects and assemblies
- Collections
- Generics
We were finally given 2 exercises.
1. Print the following string "abcdefgh" and shift it by one character until completely looped through, example output :
abcdefgh
bcdefgha
cdefghab
defghabc
etc...
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Exercises { class StringBuilderExercise { static void Main(string[] args) { StringBuilder str = new StringBuilder("The Quick Brown Fox Jumped Over The Lazy Dogs"); // run once through all the characters for (int i = 0; i < str.Length; i++) { //print the whole string starting from the current char until the char before (loop) Console.WriteLine(str); str.Append(str.ToString().Substring(0,1)); str.Remove(0,1); } } } }2. - User interactively enters a word then enter - word goes in a queue - list of words is printed - queue size is 5, meaning oldest word gets ditched if the queue is full
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Exercises { class QueueExercise { static void Main(string[] args) { Queuequeue = new Queue (); while (true) { String typedString = Console.ReadLine(); queue.Enqueue(typedString); // queue should have only 5 elements if (queue.Count == 6) { queue.Dequeue(); } PrintValues(queue); } } private static void PrintValues(IEnumerable myCollection) { foreach (Object obj in myCollection) Console.Write(" {0}", obj); Console.WriteLine(); } } }
Commentaires
1. Le jeudi 9 juin 2011 à 15:30, par Nicolas
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.