Compare commits

...

3 Commits

  1. 86
      Program.cs

86
Program.cs

@ -3,16 +3,94 @@
class Program {
static void Main(string[] args) {
// 1.
{
int n = 1; //number we will manipulate
String commands = Console.ReadLine(); //get string of "commands"
for(int i=0; i<commands.Length; ++i){
if(commands[i] == '+'){
++n;
}else if(commands[i] == '-'){
--n;
}else if(commands[i] == '0'){
n=0;
}
}
Console.WriteLine(n);
// 2.
{
n = 1; //reset number
commands = Console.ReadLine(); //ask for new "commands"
for(int i=0; i<commands.Length; ++i){
if(commands[i] == '+'){
++n;
}else if(commands[i] == '-'){
--n;
}else if(commands[i] == '0'){
n=0;
}else if(commands[i] == '!'){
for(int j=n-1; j>1; --j){ //goes from the number to one (excluding the number and one)
n = n*j; //multiplies each number one by one into the final product (it is unnecesarry to multiply by 1)
}
}
}
Console.WriteLine(n);
// 3.
{
String input = Console.ReadLine();
for(int i=1; i<input.Length; i+=2){ //starting at the second character and going through the string by steps of two
Console.Write(input[i]);
}
Console.WriteLine(); //just to be on a new line
// 4.
{
String secondWords = ""; //this string will be filled with every second word
String[] words = input.Split(' ',StringSplitOptions.RemoveEmptyEntries); //this is cheating, I just split the string into a list of strings, cutting at spaces and excluding resulting empty strings
for(int i=1; i<words.Length; i+=2){ //just like before going through every second
secondWords+=words[i]; //adding every second into the new string (there will be no spaces between words, but that was not part of the excercise and can easily be added to the code)
}
Console.WriteLine(secondWords);
// 5.
String s = Console.ReadLine(); //getting a new string just for fun
n = int.Parse(Console.ReadLine()); //getting number, and making it a number
for(int i=n-1; i<s.Length; i+=n){ //going through starting at the 'nth' char and going by steps of n
Console.Write(s[i]);
}
Console.WriteLine(); //just to begin a new line
// 6-7.
s = ' '+s; //the proper way: starting with an additional space
int wc = 0; //counting words
for(int i=1; i<s.Length; ++i){
if(s[i] != ' ' && s[i-1] == ' '){ //if there's a non-space after a space, it is a new word
++wc;
if(wc == n){ //when the required number of words is reached
for(int j = i; j<s.Length; ++j){ //6. print the string from the character that starts the 'nth' word
Console.Write(s[j]);
}
Console.WriteLine();
for(int j = i; s[j]!=' '; ++j){ //7. print the nth word finishing when a space terminates the word
Console.Write(s[j]);
}
break; //breaking the loop, so that the program does not look for the 'nth' word anymore
}
}
}
Console.WriteLine(); //new line
// 8.
String date = Console.ReadLine(); //get date
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; //ordered list of months (I hope)
int mn = int.Parse(date[3].ToString() + date[4]); //making the two M characters of the string first one string than an actual number ("10" != 10)
string output = ""; //this string will be filled with the output
for(int i=0; i<3; ++i){ //putting DD. into the output
output+=date[i];
}
output+=months[mn-1]; //putting the right month into the output (the -1 is because the indexing of the list starts from 0)
for(int i=5; i<10; ++i){ //putting .YYYY into the output
output+=date[i];
}
Console.WriteLine(output);
Console.ReadLine(); //waiting "enter"
}
}
Loading…
Cancel
Save