Decryption negating special character like " !##$%^&*()" - c#

I was wondering how am i able to negate away the special character.
For example when i key aaa for my decrypted text and move 3 space behind, it shows it ^^^ instead of www. Is there anything wrong with my coding?
This is all the code that i have inside my c#
static void Main(string[] args)
{
string UserInput = "";
int shift;
Shift OBSHIFT = new Shift();
Console.WriteLine("Welcome to Ceasar Shift Program:");
Console.WriteLine("********************************");
Console.WriteLine("type a string to encrypt:");
UserInput = Console.ReadLine();
Console.WriteLine("How many chars would you like to shift?: :");
shift = int.Parse(Console.ReadLine());
Console.Write("Your encrypted string is: ");
Console.WriteLine(OBSHIFT.Decrypt(UserInput, -shift));
Console.Read();
}
class Shift
{
public string Decrypt(string cipherString, int shift)
{
string userOutput = "";
char[] a = cipherString.ToCharArray();
for (int i = 0; i < cipherString.Length; i++)
{
char c = a[i];
int temp;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
temp = (int)(a[i] + shift);
if ((c >= 'A' && c <= 'Z' && temp > 'Z') || (c >= 'a' && c <= 'z' && temp > 'z'))
temp = temp + 26;
else
temp = (int)(a[i] + (shift));
}
else
temp = c;
userOutput += (char)temp;
}
return userOutput;
}
}

The problem is with this piece of code which does the actual shifting:
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
temp = (int)(a[i] + shift);
if ((c >= 'A' && c <= 'Z' && temp > 'Z') || (c >= 'a' && c <= 'z' && temp > 'z'))
temp = temp + 26;
else
temp = (int)(a[i] + (shift));
}
I would suggest restructuring in a few ways:
Make sure your shift is always positive to start with, so you only need to worry about going "past" the end of a particular set of characters
Use two separate branches, one for a-z and one for A-Z
Use a StringBuilder to build your string instead of repeated string concatenation
So for example:
public string ShiftText(string input, int shift)
{
// This makes sure the shift is *always* in the range 0-25.
shift = ((shift % 26) + 26) % 26;
StringBuilder output = new StringBuilder();
foreach (char c in input)
{
if (c >= 'a' && c <= 'z')
{
// We'll sort this later
}
else if (c >= 'A' && c <= 'Z')
{
// We'll sort this later
}
else
{
output.Append(c);
}
}
return output.ToString();
}
Now the bits inside the "sort this later" are simpler to work with. For the a-z part, for example:
int shifted = c + shift;
if (shifted > 'z')
{
shifted -= 26;
}
output.Append((char) shifted);
... and then the same for A-Z, but testing against 'Z' instead of 'z'. Yes, this is effectively duplicate code - but it's only there twice, and I think it's simpler to have that duplication than extract it out.

I would suggest going with this type of code instead:
public class Shift
{
public string Decrypt(string cipherString, int shift)
{
var alphabet =
Enumerable
.Concat(
Enumerable.Range((int)'a', 26),
Enumerable.Range((int)'A', 26))
.Select(i => (char)i)
.ToArray();
var map =
alphabet
.Zip(
alphabet.Concat(alphabet).Concat(alphabet).Skip(alphabet.Length + shift),
(c0, c1) => new { c0, c1 })
.ToDictionary(x => x.c0, x => x.c1);
return String.Join("", cipherString.Select(x => map.ContainsKey(x) ? map[x] : x));
}
}
The three parts are nicely distinct. (1) the alphabet is built. It can contain any number of characters, so long as there are no repeats. (1) the map is simply a dictionary built by mapping the alphabet to the alphabet shifted by shift characters. (3) returns the input cypherString mapped by the map where the key exists.

Related

My int 'LastTwoChars' is resulting in a "StartIndex cannot be less than zero." Exception. I understand the exception, but am unable to resolve it

I want to start by saying I am very new (1 week) into learning C#, so I sincerely apologize if this question is obvious. I do understand the reason for the exception, diverse.Length has to be 0 or greater. However, I am required to have the formula in my code so as to get the numbered-positions of the last 2 characters of an ever changing string (diverse).
Below, are 3 sets of code....
Firstly my working method.
static void Main(string[] args)
{
try
{
// Sample data - inputs 3 ints.
Console.WriteLine(Solution1(6, 1, 1));
Console.WriteLine(Solution1(1, 3, 1));
Console.WriteLine(Solution1(0, 1, 8));
Console.WriteLine(Solution1(5, 2, 4));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static string Solution1(int A, int B, int C)
{
string a = "a"; // a/b/c are added to string diverse when needed.
string b = "b";
string c = "c";
string diverse = "";
int totalLength = A + B + C; // Length of all 3 arrays
for(int i = 1; i <= totalLength; i++)
{
if (A >= B && A >= C && A > 0) { diverse = diverse + a; A = A - 1; }
if (B >= A && B >= C && B > 0) { diverse = diverse + b; B = B - 1; }
if (C >= A && C >= B && C > 0) { diverse = diverse + c; C = C - 1; }
}
return diverse;
}
What I am trying to do is add an additional check to my code. This check will take the printed letters and check to see if 2 of the same letter has previously been printed. If so, it will not print a 3rd. To do this I made a solution that would find the last 2 characters of the string (as I mentioned above) and compare it to the conditional check in the if statement.
Below is the code with this additional check, that I need to get working...
static string Solution1(int A, int B, int C)
{
string a = "a"; // a/b/c are added to string diverse when needed.
string b = "b";
string c = "c";
string diverse = "";
int totalLength = A + B + C; // Length of all 3 arrays
for (int i = 1; i <= totalLength; i++)
{
// Finds the last 2 characters in the diverse string.
int LastTwoChars = diverse.Length - 2;
string twoCharCheck = diverse.Substring(LastTwoChars, 2);
if (A > 0 && B < 2 && C < 2 && twoCharCheck != "aa")
{
diverse = diverse + a; A = A - 1;
}
if (B > 0 && A < 2 && C < 2 && twoCharCheck != "bb")
{
diverse = diverse + b; B = B - 1;
}
if (C > 0 && B < 2 && A < 2 && twoCharCheck != "cc")
{
diverse = diverse + c; C = C - 1;
}
}
return diverse;
}
You can have the string check only after you have at least 2 characters:
int LastTwoChars = diverse.Length - 2;
string twoCharCheck = LastTwoChars >= 0 ? diverse.Substring(LastTwoChars, 2) : string.Empty; // identical with if (LastTwoChars >= 0) twoCharCheck = diverse.Substring(LastTwoChars, 2); else twoCharCheck = string.Empty
if (A > 0 && B < 2 && C < 2 && (LastTwoChars < 0 || twoCharCheck != "aa")) // check the end only if you have at least 2 chars;
{
...
}
....
As you can see the code gets ugly very fast, so I would propose using a helper method:
// returns the last two chars in a string format, or a default user string
private static string LastTwoCharsOrDefault(string input, string default)
{
var lastTwoCharsIdx = input.Length - 2;
if (lastTwoCharsIdx > 0 )
{
return input.Substring(lastTwoCharsIdx, 2);
}
// we don't have at least 2 chars, so lets just return the default
return default;
}
Then you can change your code like this:
if (A > 0 && B < 2 && C < 2 && string.Equals("aa", LastTwoCharsOrDefault(diverse, "aa"), StringComparison.OrdinalIgnoreCase))
...
You can find more about string comparison here.
If I read your code right, you initialize Diverse as: string diverse = "";
and then execute string twoCharCheck = diverse.Substring(LastTwoChars, 2);
LastTwoChars is going to be -2 the first time through the loop.

how to transfer between arrays in c#?

I have a project in Automata theory, and I want to convert an algorithm into programming language( no matter what programming language used to solve this question)
Well, I have these orders
we enter a string and then we convert it to char so we can compare,
for example we entered this input
01001
to make it clear, I will add more inputs to know what I mean,
01000001
{valid input}
101101
{Wrong input}
0
{Wrong input}
we will check if the first digit is 0 if yes, we copy this string to another array 1, and then convert it to with 1 if it's same we copy it to array 2, then here we will need to enter a loop using for or foreach, we want to check if the next values are 0 ( without last char) so I used a.length-1 to make sure it's won't use last one, and when we see new value which is 1, we move to the last array and must be 1.
we will message using Console.WriteLine(""); that the program follows the order
I made something similar which is this (in C#):
string s= Console.ReadLine();
var a = s.ToCharArray();
Console.WriteLine("Original String: {0}",s);
//for (int k = 4; k < a.Length; k++) {
if (a[0] == '0' && a[1] == '1' && a[a.Length-1] == '1') {
for (int r = 2; r < a.Length-1; r++) {
if (a[r] == '0')
{
Console.WriteLine("The Language is Right.");
}
}
}
how do I make new arrays and copy and use it to check out
The straightforward way of implementing a finite automaton is to loop over the input and store the state in a variable:
string s= Console.ReadLine();
int state = 0;
foreach (char c in s.ToCharArray())
{
if (state == 0 && c == '0')
state = 1;
else if (state == 0 && c == '1')
state = -1;
else if (state == 1 && c == '1')
state = 2;
else if (state == 1 && c == '0')
state = -1;
else if (state == 2 && c == '1')
state = 3;
else if (state == 3)
state = -1;
}
bool accepted = state == 3;
Console.WriteLine(accepted);

And operation on hexadecimal strings

I have two Hex strings of length 8 digits. How to do & operation on these two and get result in Hex:
40003019 & FE000000 => 40000000
I have tried converting Hex string to byte array and doing individual
byte & byte which result in byte, in which case I am getting 4 bytes individually. I am looking for direct Hex result.
How can I do this?
var byteResult = new byte[4];
var byteArray1 = StringToByteArray("40003019");
var byteArray2 = StringToByteArray("FE000000");
for(int i = 0 ; i< 4; i++)
{
byteResult[i] = (byte)(byteArray1[i] & byteArray1[i]);
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
Convert (to integer), Compute (bitwise and - &), Convert (back to string):
string left = "40003019";
string right = "FE000000";
string result =
(Convert.ToUInt32(left, 16) & Convert.ToUInt32(right, 16)).ToString("X8");
In general case (long strings which can be beoynd UInt64 range)
private static int CharToByte(char value) {
if (value >= '0' && value <= '9')
return value - '0';
else if (value >= 'a' && value <= 'f')
return value - 'a' + 10;
else if (value >= 'A' && value <= 'F')
return value - 'A' + 10;
else
return -1;
}
private static string BitwiseAnd(String left, String right) {
// left and right can have different lengths
int n = Math.Min(left.Length, right.Length);
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; ++i) {
int v = CharToByte(left[left.Length - n + i]) &
CharToByte(right[right.Length - n + i]);
sb.Append(v.ToString("X1"));
}
return sb.ToString();
}
...
string left = "40003019";
string right = "FE000000";
string result = BitwiseAnd(left, right);

Generating URL-friendly unique string with embedded datetime

I'm personally tired of reimplementing "a ticket" mechanism for my web projects for some one-time operations like account activation or password reset. I know it's simple but it requires me to keep (and persist!) two pieces of data: a ticket itself and an expiration date.
So I have this idea, to generate a unique string with embedded datetime (expiration date) in it, which we can check upon receiving as a part of url-request.
I've started with this:
var clearTicket = Convert.ToInt64(expiration.ToString("yyyyMMddhhmm")).ToString("x12") + key.ToString("N");
But I want it to be more compact. Something BaseXX-ish I suppose.
Any ideas how to implement this encoding/decoding efficiently (considering url-safe charset)?
It took me some time, so I hope it helps:
First of all, you should substract 200000000000 from "yyyyMMddhhmm", because you actually don't need the first two digits for the next 88 years.
Here is the implementation for encoding and decoding Base64, using only URL-safe characters.
If you have any questions, feel free to ask.
public string Base64Encode (Int64 Number)
{
string HelpString = "";
if (Number >= 64)
{
HelpString = Base64Encode(Number / 64);
}
return (HelpString += Base64EncodeHelper(Number % 64));
}
public string Base64EncodeHelper(Int64 Number)
{
string HelpString = "";
Number += 65;
if ((Number >= 65 && Number <= 90) || (Number >= 97 && Number <= 122)) // 0 - 25 and 32 - 57
{
HelpString = Convert.ToString((char)Number);
}
else if (Number >= 91 && Number <= 96) // 26 - 31
{
HelpString = Convert.ToString((char)(Number - 43));
}
else if (Number >= 123 && Number <= 126) // 58 - 61
{
HelpString = Convert.ToString((char)(Number - 69));
}
else if (Number == 127) // 62
{
HelpString = "-";
}
else // 63
{
HelpString = "_";
}
return (HelpString);
}
public Int64 Base64Decode(string Encoded)
{
Int64 Result = 0, HelpInt = 0;
int i = Encoded.Length - 1;
foreach (char Character in Encoded)
{
int CharInInt = (int)Character;
if (Character == '_')
{
HelpInt = 63;
}
else if (Character == '-')
{
HelpInt = 62;
}
else if (((CharInInt + 69) >= 123) && ((CharInInt + 69) <= 126))
{
HelpInt = CharInInt + 4;
}
else if (((CharInInt + 43) >= 91) && ((CharInInt + 43) <= 96))
{
HelpInt = CharInInt - 22;
}
else
{
HelpInt = CharInInt - 65;
}
Result += Convert.ToInt64((Math.Pow(64, Convert.ToDouble(i))) * HelpInt);
i--;
}
return Result;
}
You can extend string like this:
public static string ToURLParameter(this string text)
{
if (String.IsNullOrEmpty(text)) return "";
// to lowercase, trim extra spaces
text = text.Trim();
var len = text.Length;
var sb = new StringBuilder(len);
bool prevdash = false;
char c;
//
text = text.Replace('Å', 'A');
text = text.Replace('å', 'a');
text = text.Replace('Ä', 'A');
text = text.Replace('ä', 'a');
text = text.Replace('Ö', 'O');
text = text.Replace('ö', 'o');
for (int i = 0; i < text.Length; i++)
{
c = text[i];
if (c == ' ' || c == ',' || c == '.' || c == '/' || c == '\\' || c == '-')
{
if (!prevdash)
{
sb.Append('-');
prevdash = true;
}
}
else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'))
{
sb.Append(c);
prevdash = false;
}
if (i == 80) break;
}
text = sb.ToString();
// remove trailing dash, if there is one
if (text.EndsWith("-"))
text = text.Substring(0, text.Length - 1);
return text;
}
and then you can use it on any variable that is string like:
string something = "asdfljasdklf";
<%= something.ToUrlParameter() %>
I've finally created a solution using techniques described in these answers:
Compressing big number (or string) to small value
How do you convert Byte Array to Hexadecimal String, and vice versa?
Works great!
Thanks you all for your help!

Iterating through the Alphabet - C# a-caz

I have a question about iterate through the Alphabet.
I would like to have a loop that begins with "a" and ends with "z". After that, the loop begins "aa" and count to "az". after that begins with "ba" up to "bz" and so on...
Anybody know some solution?
Thanks
EDIT: I forgot that I give a char "a" to the function then the function must return b. if u give "bnc" then the function must return "bnd"
First effort, with just a-z then aa-zz
public static IEnumerable<string> GetExcelColumns()
{
for (char c = 'a'; c <= 'z'; c++)
{
yield return c.ToString();
}
char[] chars = new char[2];
for (char high = 'a'; high <= 'z'; high++)
{
chars[0] = high;
for (char low = 'a'; low <= 'z'; low++)
{
chars[1] = low;
yield return new string(chars);
}
}
}
Note that this will stop at 'zz'. Of course, there's some ugly duplication here in terms of the loops. Fortunately, that's easy to fix - and it can be even more flexible, too:
Second attempt: more flexible alphabet
private const string Alphabet = "abcdefghijklmnopqrstuvwxyz";
public static IEnumerable<string> GetExcelColumns()
{
return GetExcelColumns(Alphabet);
}
public static IEnumerable<string> GetExcelColumns(string alphabet)
{
foreach(char c in alphabet)
{
yield return c.ToString();
}
char[] chars = new char[2];
foreach(char high in alphabet)
{
chars[0] = high;
foreach(char low in alphabet)
{
chars[1] = low;
yield return new string(chars);
}
}
}
Now if you want to generate just a, b, c, d, aa, ab, ac, ad, ba, ... you'd call GetExcelColumns("abcd").
Third attempt (revised further) - infinite sequence
public static IEnumerable<string> GetExcelColumns(string alphabet)
{
int length = 0;
char[] chars = null;
int[] indexes = null;
while (true)
{
int position = length-1;
// Try to increment the least significant
// value.
while (position >= 0)
{
indexes[position]++;
if (indexes[position] == alphabet.Length)
{
for (int i=position; i < length; i++)
{
indexes[i] = 0;
chars[i] = alphabet[0];
}
position--;
}
else
{
chars[position] = alphabet[indexes[position]];
break;
}
}
// If we got all the way to the start of the array,
// we need an extra value
if (position == -1)
{
length++;
chars = new char[length];
indexes = new int[length];
for (int i=0; i < length; i++)
{
chars[i] = alphabet[0];
}
}
yield return new string(chars);
}
}
It's possible that it would be cleaner code using recursion, but it wouldn't be as efficient.
Note that if you want to stop at a certain point, you can just use LINQ:
var query = GetExcelColumns().TakeWhile(x => x != "zzz");
"Restarting" the iterator
To restart the iterator from a given point, you could indeed use SkipWhile as suggested by thesoftwarejedi. That's fairly inefficient, of course. If you're able to keep any state between call, you can just keep the iterator (for either solution):
using (IEnumerator<string> iterator = GetExcelColumns())
{
iterator.MoveNext();
string firstAttempt = iterator.Current;
if (someCondition)
{
iterator.MoveNext();
string secondAttempt = iterator.Current;
// etc
}
}
Alternatively, you may well be able to structure your code to use a foreach anyway, just breaking out on the first value you can actually use.
Edit: Made it do exactly as the OP's latest edit wants
This is the simplest solution, and tested:
static void Main(string[] args)
{
Console.WriteLine(GetNextBase26("a"));
Console.WriteLine(GetNextBase26("bnc"));
}
private static string GetNextBase26(string a)
{
return Base26Sequence().SkipWhile(x => x != a).Skip(1).First();
}
private static IEnumerable<string> Base26Sequence()
{
long i = 0L;
while (true)
yield return Base26Encode(i++);
}
private static char[] base26Chars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
private static string Base26Encode(Int64 value)
{
string returnValue = null;
do
{
returnValue = base26Chars[value % 26] + returnValue;
value /= 26;
} while (value-- != 0);
return returnValue;
}
The following populates a list with the required strings:
List<string> result = new List<string>();
for (char ch = 'a'; ch <= 'z'; ch++){
result.Add (ch.ToString());
}
for (char i = 'a'; i <= 'z'; i++)
{
for (char j = 'a'; j <= 'z'; j++)
{
result.Add (i.ToString() + j.ToString());
}
}
I know there are plenty of answers here, and one's been accepted, but IMO they all make it harder than it needs to be. I think the following is simpler and cleaner:
static string NextColumn(string column){
char[] c = column.ToCharArray();
for(int i = c.Length - 1; i >= 0; i--){
if(char.ToUpper(c[i]++) < 'Z')
break;
c[i] -= (char)26;
if(i == 0)
return "A" + new string(c);
}
return new string(c);
}
Note that this doesn't do any input validation. If you don't trust your callers, you should add an IsNullOrEmpty check at the beginning, and a c[i] >= 'A' && c[i] <= 'Z' || c[i] >= 'a' && c[i] <= 'z' check at the top of the loop. Or just leave it be and let it be GIGO.
You may also find use for these companion functions:
static string GetColumnName(int index){
StringBuilder txt = new StringBuilder();
txt.Append((char)('A' + index % 26));
//txt.Append((char)('A' + --index % 26));
while((index /= 26) > 0)
txt.Insert(0, (char)('A' + --index % 26));
return txt.ToString();
}
static int GetColumnIndex(string name){
int rtn = 0;
foreach(char c in name)
rtn = rtn * 26 + (char.ToUpper(c) - '#');
return rtn - 1;
//return rtn;
}
These two functions are zero-based. That is, "A" = 0, "Z" = 25, "AA" = 26, etc. To make them one-based (like Excel's COM interface), remove the line above the commented line in each function, and uncomment those lines.
As with the NextColumn function, these functions don't validate their inputs. Both with give you garbage if that's what they get.
Here’s what I came up with.
/// <summary>
/// Return an incremented alphabtical string
/// </summary>
/// <param name="letter">The string to be incremented</param>
/// <returns>the incremented string</returns>
public static string NextLetter(string letter)
{
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (!string.IsNullOrEmpty(letter))
{
char lastLetterInString = letter[letter.Length - 1];
// if the last letter in the string is the last letter of the alphabet
if (alphabet.IndexOf(lastLetterInString) == alphabet.Length - 1)
{
//replace the last letter in the string with the first leter of the alphbat and get the next letter for the rest of the string
return NextLetter(letter.Substring(0, letter.Length - 1)) + alphabet[0];
}
else
{
// replace the last letter in the string with the proceeding letter of the alphabet
return letter.Remove(letter.Length-1).Insert(letter.Length-1, (alphabet[alphabet.IndexOf(letter[letter.Length-1])+1]).ToString() );
}
}
//return the first letter of the alphabet
return alphabet[0].ToString();
}
just curious , why not just
private string alphRecursive(int c) {
var alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
if (c >= alphabet.Length) {
return alphRecursive(c/alphabet.Length) + alphabet[c%alphabet.Length];
} else {
return "" + alphabet[c%alphabet.Length];
}
}
This is like displaying an int, only using base 26 in stead of base 10. Try the following algorithm to find the nth entry of the array
q = n div 26;
r = n mod 26;
s = '';
while (q > 0 || r > 0) {
s = alphabet[r] + s;
q = q div 26;
r = q mod 26;
}
Of course, if you want the first n entries, this is not the most efficient solution. In this case, try something like daniel's solution.
I gave this a go and came up with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Alphabetty
{
class Program
{
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
static int cursor = 0;
static int prefixCursor;
static string prefix = string.Empty;
static bool done = false;
static void Main(string[] args)
{
string s = string.Empty;
while (s != "Done")
{
s = GetNextString();
Console.WriteLine(s);
}
Console.ReadKey();
}
static string GetNextString()
{
if (done) return "Done";
char? nextLetter = GetNextLetter(ref cursor);
if (nextLetter == null)
{
char? nextPrefixLetter = GetNextLetter(ref prefixCursor);
if(nextPrefixLetter == null)
{
done = true;
return "Done";
}
prefix = nextPrefixLetter.Value.ToString();
nextLetter = GetNextLetter(ref cursor);
}
return prefix + nextLetter;
}
static char? GetNextLetter(ref int letterCursor)
{
if (letterCursor == alphabet.Length)
{
letterCursor = 0;
return null;
}
char c = alphabet[letterCursor];
letterCursor++;
return c;
}
}
}
Here is something I had cooked up that may be similar. I was experimenting with iteration counts in order to design a numbering schema that was as small as possible, yet gave me enough uniqueness.
I knew that each time a added an Alpha character, it would increase the possibilities 26x but I wasn't sure how many letters, numbers, or the pattern I wanted to use.
That lead me to the code below. Basically you pass it an AlphaNumber string, and every position that has a Letter, would eventually increment to "z\Z" and every position that had a Number, would eventually increment to "9".
So you can call it 1 of two ways..
//This would give you the next Itteration... (H3reIsaStup4dExamplf)
string myNextValue = IncrementAlphaNumericValue("H3reIsaStup4dExample")
//Or Loop it resulting eventually as "Z9zzZzzZzzz9zZzzzzzz"
string myNextValue = "H3reIsaStup4dExample"
while (myNextValue != null)
{
myNextValue = IncrementAlphaNumericValue(myNextValue)
//And of course do something with this like write it out
}
(For me, I was doing something like "1AA000")
public string IncrementAlphaNumericValue(string Value)
{
//We only allow Characters a-b, A-Z, 0-9
if (System.Text.RegularExpressions.Regex.IsMatch(Value, "^[a-zA-Z0-9]+$") == false)
{
throw new Exception("Invalid Character: Must be a-Z or 0-9");
}
//We work with each Character so it's best to convert the string to a char array for incrementing
char[] myCharacterArray = Value.ToCharArray();
//So what we do here is step backwards through the Characters and increment the first one we can.
for (Int32 myCharIndex = myCharacterArray.Length - 1; myCharIndex >= 0; myCharIndex--)
{
//Converts the Character to it's ASCII value
Int32 myCharValue = Convert.ToInt32(myCharacterArray[myCharIndex]);
//We only Increment this Character Position, if it is not already at it's Max value (Z = 90, z = 122, 57 = 9)
if (myCharValue != 57 && myCharValue != 90 && myCharValue != 122)
{
myCharacterArray[myCharIndex]++;
//Now that we have Incremented the Character, we "reset" all the values to the right of it
for (Int32 myResetIndex = myCharIndex + 1; myResetIndex < myCharacterArray.Length; myResetIndex++)
{
myCharValue = Convert.ToInt32(myCharacterArray[myResetIndex]);
if (myCharValue >= 65 && myCharValue <= 90)
{
myCharacterArray[myResetIndex] = 'A';
}
else if (myCharValue >= 97 && myCharValue <= 122)
{
myCharacterArray[myResetIndex] = 'a';
}
else if (myCharValue >= 48 && myCharValue <= 57)
{
myCharacterArray[myResetIndex] = '0';
}
}
//Now we just return an new Value
return new string(myCharacterArray);
}
}
//If we got through the Character Loop and were not able to increment anything, we retun a NULL.
return null;
}
Here's my attempt using recursion:
public static void PrintAlphabet(string alphabet, string prefix)
{
for (int i = 0; i < alphabet.Length; i++) {
Console.WriteLine(prefix + alphabet[i].ToString());
}
if (prefix.Length < alphabet.Length - 1) {
for (int i = 0; i < alphabet.Length; i++) {
PrintAlphabet(alphabet, prefix + alphabet[i]);
}
}
}
Then simply call PrintAlphabet("abcd", "");

Categories

Resources