Lektion 4: lösningsförslag
- class Person
- {
- public string FirstName;
- public string LastName;
- public int Age;
- }
- ...
- Person p = ...;
- string summary = p.FirstName + " " + p.LastName + " är " + p.Age + " år gammal.";
- class Company
- {
- public string Name;
- public string Headquarters;
- public int Age;
- }
- Company c1 = ...;
- Company c2 = ...;
- Company merger = new Company
- {
- Name = c1.Name + "-" + c2.Name,
- Headquarters = c1.Headquarters,
- Age = 0
- };
- string s = ...;
- int count = 0;
- foreach (char c in s)
- {
- if (c == 'X')
- {
- count += 1;
- }
- }
- int[] numbers = ...;
- int largest = numbers[0];
- foreach (int i in numbers)
- {
- if (i > largest)
- {
- largest = i;
- }
- }
- string[] words = ...;
- string target = "apple";
- int position = -1;
- for (int i = 0; i < words.Length; i += 1)
- {
- string w = words[i];
- if (w == target)
- {
- position = i;
- }
- }
- string text = ...;
- int periods = 0;
- int spaces = 0;
- foreach (char c in text)
- {
- if (c == '.')
- {
- periods += 1;
- }
- else if (c == ' ')
- {
- spaces += 1;
- }
- }
- int wordCount = spaces + 1;
- int sentenceCount = periods;
- double averageWordCount = (double) wordCount / sentenceCount;
- string[] strings = ...;
- int highest = 0;
- string result = null;
- foreach (string s1 in strings)
- {
- int occurrences = 0;
- foreach (string s2 in strings)
- {
- if (s1 == s2)
- {
- occurrences += 1;
- }
- }
- if (occurrences > highest)
- {
- highest = occurrences;
- result = s1;
- }
- }