here's what i understand it would be necessary to do:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public const int N = 100;
static void Main(string[] args)
{
char[] phrase = new char[N];
int i=0;
Console.WriteLine("enter a phrase: ");
while ((i < N) && (phrase[i] != '.'))
{
phrase[i] = Convert.ToChar(Console.ReadLine());
i++;
}
while(i<N){
Console.WriteLine(+phrase[i]+" ");
i++;
}
Console.ReadLine();
}
}
}
and also I want to know if there's a way I can enter a phrase without having to press intro for every character I introduce
Unless I'm missing something you can just do this:
static void Main(string[] args)
{
Console.WriteLine("enter a phrase: ");
string phrase = Console.ReadLine();
var chars = phrase.ToCharArray(); //If you want it as a char array
}
The Console.ReadLine() will read the line until the user hits return/enter and we can store this is the string phrase. If you do want the input in a char array you can use the .ToCharArray() function on the string
Related
So I am a teacher and looking to run a little hacking/cryptography club next year.
So I am programming out solutions to a few cryptography methods. I have programmed a randomised substitution cipher but cannot work out the decrypt method with it being random.
I know I need to use letter frequency and pairs of letters of not sure how to program this perfectly.
The sentence I am using is "This is a test of alans cryptography skills"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the sentence to encrypt");
string plainText = Console.ReadLine();
string cipherText = encrypt(plainText.ToUpper());
Console.WriteLine(cipherText);
Console.ReadLine();
}
public static string encrypt(string phrase)
{
List<string> originalLetters = new List<string>();
//enter the original phrase to the list
for (int i = 0; i < phrase.Length; i++ )
{
originalLetters.Add(phrase[i].ToString());
}
//randomise the subsitutions
//all
List<string> allLetters = new List<string>{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//shuffled
List<string> shuffledLetters = new List<string>();
int times = allLetters.Count;
Random rng = new Random();
for (int i = 0; i < times;i++ )
{
int position = rng.Next(0, allLetters.Count);
shuffledLetters.Add(allLetters[position]);
Console.Write(allLetters[position] + ", ");
allLetters.RemoveAt(position);
}
Console.WriteLine();
string cipherText = "";
//now change letters into shuffled letters
string SortingLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < originalLetters.Count;i++)
{
//take the original letter
//find its position in the sorting letters
int index = SortingLetters.IndexOf(originalLetters[i]);
//use position to replace to the new string
if (index != -1)
{
//swap the letter with the index plus the key
cipherText += shuffledLetters[index];
}
else
{
cipherText += originalLetters[i];
}
}
//return the encrypted message
return cipherText;
}
public static string decrypt(string phrase)
{
//take the encrypted message
//find most frequent letter as E, focus on single letter words as either I or a
return "finalAnswer";
}
}
}
I have also included example output. I want to keep this hard for my pupils to crack but just using a set key basically makes it a Caesar cipher which I already have a solution for. I have no problem with the program displaying a range of answers that the user has to find the right one from
Any help is appreciated
Thanks
This is in C#.
My program randomly picks a string from the array and then asks if the choice is acceptable. If they select yes, they can end the program. If they select no, it re-selects and asks if the choice is acceptable again.
The problem, is that after selecting no and it repeats, it'll show the same result each time.
The Code -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
public string[] randString;
public Random rand = new Random();
public bool customerSatisfied;
static void Main(string[] args)
{
Program p = new Program();
p.PostString();
//Console.ReadLine();
}
public void PostString()
{
randString = new string[]
{
"Apple",
"Orange",
"Banana",
"Pear",
"Grape",
"Cherry",
};
int randStringIndex = rand.Next(randString.Length);
string selectedString = randString[randStringIndex];
while (customerSatisfied == false)
{
Console.WriteLine("Selecting a Random Fruit");
Console.WriteLine("The Pick is " + selectedString);
Console.WriteLine("Is this acceptable?");
string userInput = Console.ReadLine();
if (userInput == "yes")
{
Console.WriteLine("The Program is finished. Press any Key to Close ");
Console.ReadLine();
customerSatisfied = true;
}
else if (userInput == "no")
{
Console.WriteLine("Making Another Attempt ");
Console.WriteLine("The Pick is " + selectedString);
Console.WriteLine("Is this acceptable?");
Console.ReadLine();
}
}
}
}
}
The problem is obvious, put the next lines inside the while loop:
int randStringIndex = rand.Next(randString.Length);
string selectedString = randString[randStringIndex];
Otherwise the selected string will be always the same.
I have problem with the .length code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string Str_Basics = "AIKdepNCZSIDETe";
int Long_Str_Bas;
string Sub_Str_1;
string Sub_Str_2;
Long_Str_Bas = Str_Basics.Length;
//Provide value for M
int M = 0;
Console.WriteLine("Provide value for M");
M = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < Long_Str_Bas; i++) ;
// First substring
Sub_Str_1 = Str_Basics.Substring(1, (M - 1));
// Second substring
Sub_Str_2 = Str_Basics.Substring((M + 1),Long_Str_Bas);
Console.WriteLine("Substring is " + Sub_Str_1);
Console.WriteLine("Substring is " + Sub_Str_2);
Console.ReadKey();
}
}
I do not know how to transfer Str_Basics.Length into a cordinates of Sub_Str_2 if anyone could explain me how does the .Length works I would be really thankful.
It sounds like you just want to do this:
// First substring
Sub_Str_1 = Str_Basics.Substring(0, M);
// Second substring
Sub_Str_2 = Str_Basics.Substring(M);
I am trying to learn C#. I want to enter some text and for it to come out reversed. It reverses it, but multiple times, as many times as the inputted text is long. So hello comes out as olleholleholleholleholleh.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Reversed_Array
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter text to be reversed");
string inputText = Console.ReadLine();
char[] myChar = inputText.ToCharArray();
Array.Reverse(myChar);
foreach (char character in myChar)
{
Console.Write(myChar);
}
Console.ReadLine();
}
}
}
I wanted to experiment with converting a string into a char array. Thought I would note this because yes I don't need the char array.
Because every time you write the whole array not a single character, try this:
foreach (char character in myChar)
{
Console.Write(character);
}
for( int i = myChar.Length -1 ; i >= 0 ; --i )
{
Console.Write(myChar[i]);
}
There's no need to have a special array, do a reverse etc., just print out characters backward:
static void Main(string[] args)
{
Console.WriteLine("Enter text to be reversed");
string inputText = Console.ReadLine();
// Backward loop
for (int i = inputText.Length - 1; i >= 0; --i)
Console.Write(inputText[i]);
}
I can't figure out how to pass total, sale and comm into Main().
Anybody got an idea how to get those variables into Main and display (output) them there with the names?
Right now I can just output the variables in calcComm ...
Thanks in advance
Philip
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication38
{
class Program
{
public static void getsales()
{
string inputsales;
double total = 0;
double sale = 0;
for (int salecount = 1; salecount <= 3; ++salecount)
{
Console.WriteLine("Enter sale: ");
inputsales = Console.ReadLine();
sale = Convert.ToDouble(inputsales);
total = total + sale;
}
calcComm(total);
}
public static void calcComm(double total)
{
double comm = 0;
comm = total * 0.2;
Console.WriteLine(comm);
}
public static void Main ()
{
Console.WriteLine(" Sunshine Hot Tubs \n Sales Commissions Report\n");
char Letter;
const string name1 = "Andreas";
const string name2 = "Brittany";
const string name3 = "Eric";
string inputLetter;
string name;
Console.WriteLine("Please enter intial or type 'z' to quit");
inputLetter = Console.ReadLine();
Letter = Convert.ToChar(inputLetter);
while (Letter != 'z')
{
if (Letter == 'a')
{
name = name1;
getsales();
}
else if (Letter == 'b')
{
name = name2;
getsales();
}
else if (Letter == 'e')
{
name = name3;
getsales();
}
else
{
Console.WriteLine("Invalid entry try again");
}
Console.WriteLine("Please enter intial or type z to quit");
inputLetter = Console.ReadLine();
Letter = Convert.ToChar(inputLetter);
}
}
}
}
This gives an array of strings corresponding to the command line parameters.
Main(string [] args)
By the way, when dealing with monetary units, it's better to use decimal than double.
You should be using objects, then you can make those public.
class Sales
{
public double total;
public double sale;
public double comm;
...
public void CalcComm()
{
...
}
}
Then you can reference them like this:
Sales.total, Sales.sale
Or you can make them global but that's not normally advisable.
Look into the return keyword in C#; get your functions to return the relevant data back to main and have it make use of it.
Consider this example for how to add command line arguments. If you need them to be programmatically added consider writing a wrapper program and starting the Process inside it.
using System;
class Program
{
static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("args is null"); // Check for null array
}
else
{
Console.Write("args length is ");
Console.WriteLine(args.Length); // Write array length
for (int i = 0; i < args.Length; i++) // Loop through array
{
string argument = args[i];
Console.Write("args index ");
Console.Write(i); // Write index
Console.Write(" is [");
Console.Write(argument); // Write string
Console.WriteLine("]");
}
}
Console.ReadLine();
}
}
either you can build up a Data Transfer Object that holds all these three variables instantiate it and then return it to your main function.
You could also make use of variables that are passed as references instead of by value and use the updated reference value. Read about pass by value type & reference type for c# and the ref keyword.