C# for loop and encryption [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm Kind of new to C# but since i have learned other programming languages before, learning it is going pretty well, However I am stuck on this one part, I am trying to "port" my old python application that takes a string or the users input and encrypts it. the python code is below, My problem is doing everything after and including the for loop. How might i go about searching for each letter in the Character string.
CHARACTER= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=+_)(*&^%$##!?><|:;}{]["
translated = ''
for uniqueKey in message:
if uniqueKey in CHARACTER:
num = CHARACTER.find(uniqueKey)
if mode == "encrypt":
num += key
elif mode == "decrypt":
num -= key
if num >= len(CHARACTER):
num -= len(CHARACTER)
elif num < 0:
num += len(CHARACTER)
translated = translated + CHARACTER[num]
else:
translated = translated + uniqueKey

I think the function you are looking for is IndexOf(). This is the equivalent of your find call above:
foreach (var uniqueKey in message)
{
var num = CHARACTER.IndexOf(uniqueKey);
if (num >= 0)
{
...
}
}

Related

Why is the for-loop choosing the wrong IF statement path? [closed]

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 3 years ago.
Improve this question
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}

how to convert Hindi numbers ( २०७४) to numeric value in c#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a large group of Hindi numbers which i want to convert into numeric values but i don't know how to convert them . Please suggest me appropriate way to achieve this.
Note Please don't suggest me replace method.
eg. convert this number २०७४ to equivalent to 2074.
I believe this is what you're after but be aware that this code is written by someone who doesn't speak Hindi, read Hindi or know Hindi.
I found the digits on the wikipedia page but I absolutely have no idea what I'm doing.
The google page (which I found by just googling for the individual digits from the original string in the question) seems to indicate the following:
The digits for 0-9 are ०१२३४५६७८९
I clicked on a link and used the last character of the url as the digit
Note that 4 had to be gotten as the second digit of 14, and there seems to be a disambiguity suffix on that link as well
They have unicode code points ranging from 2406 through 2415, in that order
The double digits numbers follow the system to a tee, so it seems to be just a 10-digit numeric system using different code points
But note that there are far too few examples for me to be absolutely certain this holds true for all numbers
If anyone pokes hole in this answer I will take it down but feel free to grab all the code from it first if you think it can be improved.
Also bear in mind that the OP explicitly asked for a non-replace method. The whole thing can probably be written in a oneliner with that but since that doesn't seem to be an acceptable answer then here we are.
With all that said, here's a non-string-replace version that mimicks basic numeric parsing using different symbols:
Note: There's about 7 tons of error-handling that isn't present here, such as empty strings, etc.
public static bool TryParseHindiToInt32(string text, out int value)
{
const int codePointForZero = 2406;
const int codePointForNine = codePointForZero + 9;
int sign = +1;
int index = 0;
if (index < text.Length && text[index] == '-') // todo: hindi minus?
{
index++;
sign = -1;
}
value = 0;
while (index < text.Length)
{
char c = text[index];
if (c < codePointForZero || c > codePointForNine)
{
value = 0;
return false;
}
if ((uint)value > 214748364u)
{
value = 0;
return false;
}
value *= 10;
value += (c - codePointForZero);
index++;
}
value *= sign;
return true;
}
Test:
string digits = "२०७४";
TryParseHindiToInt32(digits, out int i);
Console.WriteLine(i);
Outputs:
2074

Numbers only password strength [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In my organization, users must generate a password from numbers only (keypads are used to access), minimum length is 8 numbers. How can I make sure the password the user generats is not too weak (using c# on server procossing password change request), applying the following rule:
3 following numbers (even a part of password) are not sequential or repeated (9451238401 or 543555784)
The regular expression is:
^((?!(?<ch>.)\k<ch>\k<ch>)(?!012|123|234|345|456|567|678|789|890)[0-9]){8,}$
The (?!(?<ch>.)\k<ch>\k<ch>) will check for the same character repeated thrice. Note that for the various contiguous sequences I had to put them in a list of possible sequences, (?!012|123|234|345|456|567|678|789|890). [0-9] is the character that will be accepted as valid. The {8,} is for the minimum length.
If you want a general-purpose approach which tells you the number of repeated, ascending and descending digits:
static void checkStrength(string text, out int maxRepeats, out int maxAscending, out int maxDescending)
{
maxRepeats = 0;
maxAscending = 0;
maxDescending = 0;
int currRepeats = 0;
int currAscending = 0;
int currDescending = 0;
for (int i = 1; i < text.Length; ++i)
{
char curr = text[i];
char prev = text[i-1];
if (curr - prev == -1)
maxDescending = Math.Max(maxDescending, ++currDescending);
else
currDescending = 1;
if (curr - prev == 1)
maxAscending = Math.Max(maxAscending, ++currAscending);
else
currAscending = 1;
if (curr == prev)
maxRepeats = Math.Max(maxRepeats, ++currRepeats);
else
currRepeats = 1;
}
}
You would have to call this and then do what you want with the results:
int maxRepeats, maxAscending, maxDescending;
checkStrength(text, out maxRepeats, out maxAscending, out maxDescending);
if (maxRepeats > REPEAT_LIMIT || maxAscending > ASCENDING_LIMIT || maxDescending > DESCENDING_LIMIT)
{
// ... report error or whatever
}
If you don't need to vary the allowed number of repeated or ascending digits, then xanatos' regex is clearly by far the shortest code. This code is only needed if you need to vary the allowed counts at runtime.

I'm trying to do a Prime Number finder but cant see why it's not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to do a Prime Number finder but cant see why it's not working.
When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
int maxPrime = 11; //The maximum found Primes
int primeCount = primes.Count; //Current Number of Primes
int num = 4; //Current Number
int x = 0; //
int curPrime = primes[x];
while (primeCount < maxPrime)
{
if (x != primeCount)
{
if (num % primes[x] == 0)
{
num++;
x = 0;
}
else
{
x++;
}
}
else
{
primes.Add(num);
primeCount=primes.Count;
x = 0;
}
}
primes.ForEach(i => Console.Write("{0}\t", i));
You have an infinite loop.
Since you never modify primeCount or maxPrime, this will always be true:
while (primeCount < maxPrime)
In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.
(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)

C# building permutations with wildcards using recursion [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm having a mind melt.
The problem I'm having is that I need to be able to do the following:
I have an input that defines how many wildcards I can use UP TO, in this example we say 2.
I have a string, ABCDEFGH.
I need to create an array of all the different ways those 2 wildcards can feature in the string so that I can feed it into an SQL query.
By hand we can do.
_BCDEFGH
A_CDEFGH
AB_DEFGH
ABC_EFGH
ABCD_FGH
ABCDE_GH
ABCDEF_H
ABCDEFG_
And using our limit of 2.
__CDEFGH
_B_DEFGH
_BC_EFGH
_BCD_FGH
_BCDE_GH
_BCDEF_H
_BCDEFG_
A__DEFGH
A_C_EFGH
A_CD_FGH
A_CDE_GH
A_CDEF_H
A_CDEFG_
AB__EFGH
AB_D_FGH
and so on...
For compatability with SQL I need to use the wildcard character as an underscore_.
Can someone help me understand how to structure my loops? Bare in mind that limit of wildcards isn't always 2, it is a variable.
This isn't a question of Regex or matching, I need to be able to create these variations of a string.
you get something like this
List<String> permutations(String original, int numberOfWildcards) {
//add 1 more wildcard to each posible position in the original string
List<String> perm = new List<String>();
for (int i = 0; i < original.Length; ++i)
{
if (original[i] != '_')
perm.Add(original.Substring(0, i) + "_" + original.Substring(i + 1, original.Length));
}
if ( numberOfWildcards == 1)
{
return perm;
}
//now if we need to search deeper we recusively do this for each substring
List<String> permWithMoreWildmark = new List<String>();
foreach (var str in perm)
{
permWithMoreWildmark.AddRange(permutations(str,numberOfWildcards-1));
}
return permWithMoreWildmark;
}
the trick is to try to solve the problem for 1 deep first and then try to figure out the recursion

Categories

Resources