Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am currently getting into Pentesting and Ethical Hacking to test website security.
I would appreciate an example Bruteforce algorithm that is stored in a string. Not a dictionary algorithm, but a bruteforce algorithm. For example, it tries the letter a. Then it tries the letter b, then it tries c and so on. Thank you in advance :)
Even if i think that you should really come up with an own concept for this problem, i'll like to give you a hint how to do this.
Disclaimer: this is the laziest, slowest and dirtiest approach possible but it gets its job done. NEVER EVER USE THIS FOR A REAL SYSTEM.
Programm.cs
class Program
{
static void Main(string[] args)
{
Brutforce b = new Brutforce()
{
Alphabet = new []{'a', 'b', 'c', 'd'}
};
ICollection<string> vals = b.Calculate(3);
foreach (var elem in vals)
Console.WriteLine(elem);
Console.ReadKey();
}
}
Brutforce.cs
internal class Brutforce
{
public ICollection<char> Alphabet { get; set; }
private ICollection<string> _calculate(int lenght)
{
if (lenght <= 1) return Alphabet.Select(a => a + "").ToList();
ICollection<string> sub = _calculate(lenght - 1);
return (from alpha in Alphabet from prior in sub select alpha + prior).ToList();
}
public ICollection<string> Calculate(int lenght)
{
return Alphabet == null ? null : _calculate(lenght);
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I just finished a C# totorial and thought it would be a cool idea to get the most popular type of computers using lists. I was able to get it to work exept the only the most recent element will be added to the list.
namespace computerDatabase
{
class program
{
static void Main()
{
while (true)
{
List<string> computerName = new List<string>();
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
computerName.Add(cName);
if (cName == "list")
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
else
{
computerName.Add(cName.ToLower());
}
}
}
}
}
As pointed out by Jon, you re-initializing your list for every iteration, nullifying any input added to the list. You also do not need to specify a capacity for the list in the constructor as it will grow automatically. I've corrected the relevant parts for you:
List<string> computerName = new List<string>();
while (true)
{
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
if (cName != "list")
computerName.Add(cName.ToLower());
else
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I need to convert the entered string value into an ASCII value to store into database as an array object. It is required as per the functional requirement.
You can use the below code -
static void Main(string[] args)
{
string value = "Hello World";
Console.WriteLine(value);
int[] result = new int[value.Length];
int index = 0;
foreach (var c in value)
{
if (char.IsDigit(c))
{
result[index] = Convert.ToInt32(c);
}
else
{
result[index] = ((int)c);
}
index++;
}
foreach (int i in result)
{
Console.Write("{0} ", i);
}
}
The if part is added due to consider if the string value contains any numerical value. That's why it will check either that particular character is digit or not. If digit then it will cast the value into Integeer type. Otherwise, else block will be called for all the characters value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Newb to C# and OOP. My journey thus far has been to take code bases that I've inherited from former developers and either address issues, or add enhancements, whilst trying to understand said code bases' structures from front-to-back.
I'm having trouble fully grasping the concept around the parameters which follow the initial declaration of a method. Here's an example of a method I'm working with:
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity entityManagement = new Entity();
string[] lineParts = line.Split('|');
entityManagement.Identifier = lineParts[0];
entityManagement.ProductId = 1234;
entityManagement.Category = "ABCDE";
entities.Add(entityManagement);
}
return entities;
}
The part after ParseCsvFile in parentheses: (List<string> entries, string urlFile)
Could someone explain what these are and what they do, perhaps with metaphors/analogies/real-world examples?
It might be easier to see their purpose if you look at a simpler function for example:
public int Add(int number1, int number2)
{
return number1 + number 2;
}
Above there is a function that adds two numbers together and returns the result. It is a set of instructions to follow. How can it follow the instructions if it doesn't know what numbers to use.
That's where calling the function comes in.
for example:
var result = Add(2, 5);
In this scenario result = 7.
2 is replacing number1 in the function and 5 is replacing number2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for reading .Net, C# reserved key words programmatically in VS 2015.
I got the answer to read C# reserved words in the [link][1].
CSharpCodeProvider cs = new CSharpCodeProvider();
var test = cs.IsValidIdentifier("new"); // returns false
var test2 = cs.IsValidIdentifier("new1"); // returns true
But for var, dynamic, List, Dictionary etc the above code is returning wrong result.
Is there any way to identify .net keywords in run time instead of listing key words in a list?
string[] _keywords = new[] { "List", "Dictionary" };
This is a perfectly fine C# program:
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int var = 7;
string dynamic = "test";
double List = 1.23;
Console.WriteLine(var);
Console.WriteLine(dynamic);
Console.WriteLine(List);
}
}
}
So your premise is wrong. You can find the keywords by looking them up in the short list. Just because something has a meaning does not mean it's in any way "reserved".
Do not let the online syntax highlighting confuse you. Copy and paste it into Visual Studio if you want to see proper highlighting.
As explained by nvoigt, your method of programmatically determining if a string is a keyword is effectively correct. To be complete, (after checking Reflector) it should be:
bool IsKeyword(string s)
{
var cscp = new CSharpCodeProvider();
return s != null
&& CodeGenerator.IsValidLanguageIndependentIdentifier(s)
&& s.Length <= 512
&& !cscp.IsValidIdentifier(s);
}
(The VB.NET version needs 1023 and a check for "_".)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a pre defined string as Follows.
string preDefined="abc"; // or i can use char array in here
string value="ac";
string value1="abw";
I need some function to compare value with preDefined.
(value.SomefunctionContains(preDefined)
this function needs to return
value -> true;
value1 -> false
I knew that i can't use contains() or Any(). so plz help
You are just looking for if value has any character that is not in predefined, so this should do it:
!value.Any(x => !predefined.Contains(x))
Or it's more clear using All:
value.All(predefined.Contains);
private bool SomeFunction(string preDefined, string str)
{
foreach (char ch in str)
{
if (!preDefined.Contains(ch))
{
return false;
}
}
return true;
}
You can implement the following method to get the result :
private static bool DoesContain(string predefined, string value)
{
char[] c_pre = predefined.ToCharArray();
char[] c_val = value.ToCharArray();
char[] intersection = c_pre.Intersect(c_val).ToArray();
if (intersection.Length == c_val.Length) {
return true;
}
else {
return false;
}
}
Please not that this solution is a generalized implementation. IT also returns true even if the characters are not in the same order, unless ther include all.