For loop exercises
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

using System;
class Program {
static int Main(string[] args) {
// 1.
{
Console.Write("E1: Enter string [+-0]: ");
string str = Console.ReadLine();
int x = 1;
foreach (char c in str) switch (c) {
case '+': ++x; break;
case '-': --x; break;
case '0': x = 0; break;
default:
Console.WriteLine("Invalid character: '"+c+"'!");
return 1;
}
Console.WriteLine(x);
}
// 2.
{
Console.Write("E2: Enter string [+-0!]: ");
string str = Console.ReadLine();
int x = 1;
foreach (char c in str) switch (c) {
case '+': ++x; break;
case '-': --x; break;
case '0': x = 0; break;
case '!':
if (x == 0) break;
int s = x < 0 ? 1 : -1;
for (int i = x+s; i != 0; i += s) x *= i;
break;
default:
Console.WriteLine("Invalid character: '"+c+"'!");
return 1;
}
Console.WriteLine(x);
}
// 3.
{
Console.Write("E3: Enter a word: ");
string str = Console.ReadLine();
for (int i = 0; i < str.Length; i += 2)
Console.Write(str[i]);
Console.WriteLine();
}
// 4.
{
Console.Write("E4: Enter words: ");
string str = Console.ReadLine();
string[] split = str.Split(' ');
for (int i = 0; i < split.Length; i += 2)
Console.Write(split[i] + ' ');
Console.WriteLine();
}
return 0;
}
}