[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# String manipulation

Posted on 2016-11-17
Medium Priority
99 Views
Hi EE,

I have to create a list(Of String) with a loop

then :
aaab
aaac

all up to aaaz
after I would like aaa0 up to aaa9
then aaba all up to aaza then numbers...

I think you know waht all mean, in other words all possibility Characters (4 of length) numeric & alpha

how can i do that ?
0
Question by:PhilippeRenaud
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4
• 2
• +3

LVL 41

Expert Comment

ID: 41892239
don't look at it as a string, look at it as a 4 char array.

just like a number system you have a starting point and an ending point.
eg
00
01
...
09 <- end point
10

So start at the right most char, increment by one.  When you hit the endpoint, increment 1 to the left, and then reset the  current slot.

eg
099 + 1
090 <-- add 1 to middle column, but need to reset again.
100

I'm assuming this is academic which is why I posted at all, but know there's a hacking technique called brute forcing which is essentially what you're programming here.
1

LVL 30

Expert Comment

ID: 41892269
Just in case, I'd like to clarify the task.
First, I believe Kyle Abrahams is right and this is academic exercise. So I don't want to provide any code.

About the task. As a result of the algorithm you have described you will get a list of (26 + 10)*4 strings. There won't be strings in the list like: aazz or zzza ot zzzz? Am I correct? Or do you need a big list of all possible combinations (quite a big number :))?
0

LVL 35

Expert Comment

ID: 41892579
(quite a big number

actually it is 1679616 4-char-words what is about 6.7 MB without spaces or 8.4 MB with one space after each word.

you could make a class AlphaNum4 which has a 4-byte array as member and a static byte array of length 36 which contains your 'alphabet'

{ 'a', 'b', ..., 'z', '0', .... '9' }

then a member function which increments the 'this' like kyle has described and returns the incremented 'number'.

then an output function which returns the AlphaNum4 as string.

with that you could have a loop from 0 to 1679616 (excluded) (or 36*36*36*36) where you increment from 'aaaa' to '9999' and write each combination to a file.

Sara
0

LVL 1

Author Comment

ID: 41892848
I knew the "hacking" comment would come up it's always the same thing on expert exchange

It's not related to this at all, neither Academic,  it's for work and we need to anotate a document and every new document they want to increase a code (they have their meaning) so it would start at aaaa and like I said increase

I can ask maybe to do this only at 3 combinaison and not 4 ... that I can do

but I need help with the code thats why I came here....
anyways.
0

LVL 1

Author Comment

ID: 41892856
Also I am a member since 2005 go in my profile
I dont think I would go to school from 2005 to 2016

neither at my age and neither at the income I do, I dont need school anymore

It's a bit annoying to always defend this fact every time we ask a question (I dont want to offend anybody) I say this with respect.
0

LVL 64

Expert Comment

ID: 41893241
Hi PhilippeRenaud;

See if this code meets your needs.
``````// Starting value of the list of strings to be generated
string startSeq = "aaaa";
// Used to build each element of the list
StringBuilder sb = new StringBuilder();
// The values to be replaced in the startSeq
string replaceWith = "bcdefghijklmnopqrstuvwxyz0123456789";
// The resulting list of values
List<string> theSeq = new List<string>();
// Add the first element to the list

// Loop through the four elements in the sequence to be replaced
for (int i = 3; i >	-1; i--)
{
// Start the next sequence
sb.Clear().Append(startSeq);
// Go through the characters that will replace the character in startSeq
foreach (char nextChar in replaceWith)
{
// Remove the original character and replace it
sb.Remove(i, 1).Insert(i, nextChar.ToString());
// Add it to the resulting sequence
}
}

// The list is now in theSeq
``````
0

LVL 41

Assisted Solution

Kyle Abrahams earned 1000 total points
ID: 41893285
Thanks for clarifying.  People do go back to school for different areas (eg: you can be a network guy back in 2005 going for programming now).
I wasn't accusing you of hacking . . . which is why I commented at all and the document reversions make sense.  You'd be surprised at how many  questions I've seen over the years of how do I get this illegally or can I break into this.  Also some things are ambiguous.  This code COULD be used for nefarious means, so taking the extra precaution to ensure you're going to use it correctly is in everyone's best interest.

Back to the question at hand.

I would actually do this as an array of integers.  Referring to an ascii table:
http://www.ascii-code.com/

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
//coded as variables for flexibility.
const int start = 97;  //a
const int end = 122;  //z
const int start2 = 48;  // 0
const int end2 = 57; //9

static void Main(string[] args)
{

int[] rev = new int[] { start, start, start, start};

for (int i = 0; i < 1000; i++)
{
Output(rev);
Increment(rev, rev.Length - 1);
}

}

static void Increment(int[] rev, int pos)
{
rev[pos]++;

// if I passed z, go to 0
if (rev[pos] == end + 1)
rev[pos] = start2;

//if I passed 9.
if (rev[pos] == end2 + 1)
{
//go back to beginning
rev[pos] = start;

// if I can increment one position to the left, do so.
if (pos - 1 >= 0)
Increment(rev, pos - 1);
}
}

static void Output(int[] x)
{
string s = "";
foreach (int i in x)
{
s += (char)i;
}
Console.WriteLine(s);

}
}
}
``````
0

LVL 34

Expert Comment

ID: 41893372
Using a Code Project by Adrian Akison, I produced this solution:

Program.cs -
``````using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28983886
{
class Program
{
static void Main(string[] args)
{
var set = new List<char>((from i in Enumerable.Range(0, 26) select (char)(i + 97)).Concat(from i in Enumerable.Range(0, 10) select (char)(i + 48)));
var variations = new Variations<char>(set, 4, GenerateOption.WithRepetition).Select(x => string.Join("", x)).ToList();
foreach (var variant in variations)
Console.WriteLine(String.Join("", variant.ToArray()));
}
}
}
``````

SupportingObjects.cs -
``````using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

// Using Combinations, Permutations and Variations implementation provided by Adrian Akison
// http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
namespace EE_Q28983886
{
public enum GenerateOption
{
WithoutRepetition,
WithRepetition
}

interface IMetaCollection<T> : IEnumerable<IList<T>>
{
long Count { get; }
GenerateOption Type { get; }
int UpperIndex { get; }
int LowerIndex { get; }
}

class SmallPrimeUtility
{
private SmallPrimeUtility() { ;}

public static List<int> Factor(int i)
{
int primeIndex = 0;
int prime = PrimeTable[primeIndex];
List<int> factors = new List<int>();
while (i > 1)
{
if (i % prime == 0)
{
i /= prime;
}
else
{
++primeIndex;
prime = PrimeTable[primeIndex];
}
}
return factors;
}

public static List<int> MultiplyPrimeFactors(IList<int> lhs, IList<int> rhs)
{
List<int> product = new List<int>(lhs.Concat(rhs));
product.Sort();
return product;
}

public static List<int> DividePrimeFactors(IList<int> numerator, IList<int> denominator)
{
return numerator.Except(denominator).ToList();
}

public static long EvaluatePrimeFactors(IList<int> value)
{
long accumulator = 1;
foreach (int prime in value)
accumulator *= prime;
return accumulator;
}

static SmallPrimeUtility()
{
CalculatePrimes();
}

private static void CalculatePrimes()
{
BitArray sieve = new BitArray(65536, true);
for (int possiblePrime = 2; possiblePrime <= 256; ++possiblePrime)
{
if (sieve[possiblePrime] == true)
{
for (int nonPrime = 2 * possiblePrime; nonPrime < 65536; nonPrime += possiblePrime)
sieve[nonPrime] = false;
}
}
// Scan sieve for primes...
if (PrimeTable == default(IList<int>))
PrimeTable = new List<int>();

for (int i = 2; i < 65536; ++i)
{
if (sieve[i])
}

}

public static IList<int> PrimeTable { get; private set; }
}

class Combinations<T> : IMetaCollection<T>
{
#region Constructors
protected Combinations() { ;}

public Combinations(IList<T> set, int lowerIndex)
{
Initialize(set, lowerIndex, GenerateOption.WithoutRepetition);
}

public Combinations(IList<T> set, int lowerIndex, GenerateOption type)
{
Initialize(set, lowerIndex, type);
}
#endregion

#region IEnumerable Interface
public IEnumerator<IList<T>> GetEnumerator()
{
return new Enumerator(this);
}

IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
#endregion

#region Enumerator Inner Class
public class Enumerator : IEnumerator<IList<T>>
{
#region Constructors
public Enumerator(Combinations<T> source)
{
parent = source;
enumerator = (Permutations<bool>.Enumerator)parent.permutations.GetEnumerator();
}
#endregion

#region IEnumerator interface
public void Reset()
{
enumerator.Reset();
}

public bool MoveNext()
{
bool ret = enumerator.MoveNext();
values = null;
return ret;
}

public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}

object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}

public void Dispose() { ;}
#endregion

#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
int index = 0;
IList<bool> currentPermutation = (IList<bool>)enumerator.Current;
for (int i = 0; i < currentPermutation.Count; ++i)
{
if (currentPermutation[i] == false)
{
if (parent.Type == GenerateOption.WithoutRepetition)
++index;
}
else
++index;
}
}
}
#endregion

#region Data
private Combinations<T> parent;
private List<T> values;
private Permutations<bool>.Enumerator enumerator;
#endregion
}
#endregion

#region IMetaList Interface
public long Count { get { return permutations.Count; } }
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get; private set; }
#endregion

#region Heavy Lifting Members
private void Initialize(IList<T> set, int lowerIndex, GenerateOption type)
{
Type = type;
LowerIndex = lowerIndex;
values = new List<T>();
List<bool> myMap = new List<bool>();
if (type == GenerateOption.WithoutRepetition)
{
for (int i = 0; i < values.Count; ++i)
myMap.Add(i >= values.Count - LowerIndex ? false : true);
}
else
{
for (int i = 0; i < set.Count - 1; ++i)

for (int i = 0; i < LowerIndex; ++i)
}
permutations = new Permutations<bool>(myMap);
}
#endregion

#region Data
private List<T> values;
private Permutations<bool> permutations;
#endregion
}

class Permutations<T> : IMetaCollection<T>
{
#region Constructors
protected Permutations() { ;}

public Permutations(IList<T> set)
{
Initialize(set, GenerateOption.WithoutRepetition, null);
}

public Permutations(IList<T> set, GenerateOption type)
{
Initialize(set, type, null);
}

public Permutations(IList<T> set, IComparer<T> comparer)
{
Initialize(set, GenerateOption.WithoutRepetition, comparer);
}
#endregion

#region IEnumerable Interface
public virtual IEnumerator GetEnumerator()
{
return new Enumerator(this);
}

IEnumerator<IList<T>> IEnumerable<IList<T>>.GetEnumerator()
{
return new Enumerator(this);
}
#endregion

#region Enumerator Inner-Class
public class Enumerator : IEnumerator<IList<T>>
{
#region Constructors
public Enumerator(Permutations<T> source)
{
parent = source;
orders = new int[source.orders.Length];
source.orders.CopyTo(orders, 0);
Reset();
}
#endregion

#region IEnumerator Interface
public void Reset()
{
position = Position.BeforeFirst;
}

public bool MoveNext()
{
if (position == Position.BeforeFirst)
{
values = new List<T>(parent.values.Count);
Array.Sort(orders);
position = Position.InSet;
}
else if (position == Position.InSet)
{
if (values.Count < 2)
position = Position.AfterLast;
else if (NextPermutation() == false)
position = Position.AfterLast;
}
return position != Position.AfterLast;
}

public object Current
{
get
{
if (position == Position.InSet)
return new List<T>(values);
else
throw new InvalidOperationException();
}
}

IList<T> IEnumerator<IList<T>>.Current
{
get
{
if (position == Position.InSet)
return new List<T>(values);
else
throw new InvalidOperationException();
}
}

public virtual void Dispose() { ;}
#endregion

#region Heavy Lifting Methods
private bool NextPermutation()
{
int i = orders.Length - 1;
while (orders[i - 1] >= orders[i])
{
--i;
if (i == 0)
return false;
}

int j = orders.Length;
while (orders[j - 1] <= orders[i - 1])
--j;

Swap(i - 1, j - 1);
++i;

j = orders.Length;
while (i < j)
{
Swap(i - 1, j - 1);
++i;
--j;
}
return true;
}

private void Swap(int i, int j)
{
current = values[i];
values[i] = values[j];
values[j] = current;
kvi = orders[i];
orders[i] = orders[j];
orders[j] = kvi;
}
#endregion

#region Data and Internal Members
private T current;
private int kvi;
private Position position = Position.BeforeFirst;
private int[] orders;
private List<T> values;
private Permutations<T> parent;

private enum Position
{
BeforeFirst,
InSet,
AfterLast
}
#endregion
}
#endregion

#region IMetaList Interface
public long Count { get; private set; }
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get { return values.Count; } }
#endregion

#region Heavy Lifting Members
private void Initialize(IList<T> set, GenerateOption type, IComparer<T> comparer)
{
Type = type;
values = new List<T>(set.Count);
orders = new int[set.Count];
if (type == GenerateOption.WithRepetition)
{
for (int i = 0; i < orders.Length; ++i)
orders[i] = i;
}
else
{
if (comparer == null)
comparer = new SelfComparer<T>();
values.Sort(comparer);

int j = 1;
if (orders.Length > 0)
orders[0] = j;

for (int i = 1; i < orders.Length; ++i)
{
if (comparer.Compare(set[i - 1], set[i]) != 0)
++j;
orders[i] = j;
}
}
Count = GetCount();
}

private long GetCount()
{
int runCount = 1;
List<int> divisors = new List<int>();
List<int> numerators = new List<int>();
for (int i = 1; i < orders.Length; ++i)
{
if (orders[i] == orders[i - 1])
++runCount;
else
{
for (int f = 2; f <= runCount; ++f)
runCount = 1;
}
}
for (int f = 2; f <= runCount; ++f)
return SmallPrimeUtility.EvaluatePrimeFactors(SmallPrimeUtility.DividePrimeFactors(numerators, divisors));
}
#endregion

#region Data and Internal Members
private List<T> values;
private int[] orders;

private class SelfComparer<U> : IComparer<U>
{
public int Compare(U x, U y)
{
return ((IComparable<U>)x).CompareTo(y);
}
}
#endregion
}

class Variations<T> : IMetaCollection<T>
{
#region Constructors
protected Variations() { ;}

public Variations(IList<T> set, int lowerIndex)
{
Initialize(set, lowerIndex, GenerateOption.WithoutRepetition);
}

public Variations(IList<T> set, int lowerIndex, GenerateOption type)
{
Initialize(set, lowerIndex, type);
}
#endregion

#region IEnumerable Interface
public IEnumerator<IList<T>> GetEnumerator()
{
if (Type == GenerateOption.WithRepetition)
return new EnumeratorWithRepetition(this);
else
return new EnumeratorWithoutRepetition(this);
}

IEnumerator IEnumerable.GetEnumerator()
{
if (Type == GenerateOption.WithRepetition)
return new EnumeratorWithRepetition(this);
else
return new EnumeratorWithoutRepetition(this);
}
#endregion

#region Enumerator Inner Class
public class EnumeratorWithRepetition : IEnumerator<IList<T>>
{
#region Constructors
public EnumeratorWithRepetition(Variations<T> source)
{
parent = source;
Reset();
}
#endregion

#region IEnumerator interface
public void Reset()
{
values = null;
indices = null;
}

public bool MoveNext()
{
int carry = 1;
if (indices == null)
{
indices = new List<int>();
for (int i = 0; i < parent.LowerIndex; ++i)
carry = 0;
}
else
{
for (int i = indices.Count - 1; i >= 0 && carry > 0; --i)
{
indices[i] += carry;
carry = 0;
if (indices[i] >= parent.UpperIndex)
{
indices[i] = 0;
carry = 1;
}
}
}
values = null;
return carry != 1;
}

public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}

object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}

public void Dispose() { ;}
#endregion

#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
foreach (int index in indices)
}
}
#endregion

#region Data
private Variations<T> parent;
private List<T> values;
private List<int> indices;
#endregion
}

public class EnumeratorWithoutRepetition : IEnumerator<IList<T>>
{
#region Constructors
public EnumeratorWithoutRepetition(Variations<T> source)
{
parent = source;
enumerator = (Permutations<int>.Enumerator)parent.permutations.GetEnumerator();
}
#endregion

#region IEnumerator interface
public void Reset()
{
enumerator.Reset();
}

public bool MoveNext()
{
bool ret = enumerator.MoveNext();
values = null;
return ret;
}

public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}

object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}

public void Dispose() { ;}
#endregion

#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
int index = 0;
IList<int> currentPermutation = (IList<int>)enumerator.Current;
for (int i = 0; i < parent.LowerIndex; ++i)

for (int i = 0; i < currentPermutation.Count; ++i)
{
int position = currentPermutation[i];
if (position != Int32.MaxValue)
{
values[position] = parent.values[index];
if (parent.Type == GenerateOption.WithoutRepetition)
++index;
}
else
++index;
}
}
}
#endregion

#region Data
private Variations<T> parent;
private List<T> values;
private Permutations<int>.Enumerator enumerator;
#endregion
}
#endregion

#region IMetaList Interface
public long Count
{
get
{
if (Type == GenerateOption.WithoutRepetition)
return permutations.Count;
else
return (long)Math.Pow(UpperIndex, LowerIndex);
}
}
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get; private set; }
#endregion

#region Heavy Lifting Members
private void Initialize(IList<T> set, int lowerIndex, GenerateOption type)
{
Type = type;
LowerIndex = lowerIndex;
values = new List<T>();
if (type == GenerateOption.WithoutRepetition)
{
List<int> myMap = new List<int>();
int index = 0;
for (int i = 0; i < values.Count; ++i)
{
if (i >= values.Count - LowerIndex)
else
}
permutations = new Permutations<int>(myMap);
}
else { /* List is not used */;}
}
#endregion

#region Data
private List<T> values;
private Permutations<int> permutations;
#endregion
}
}
``````
Which produces all of the variations of aaaa through 9999.

Sample output -
The original project is here: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

-saige-
0

LVL 1

Author Comment

ID: 41893517
Fernando, its almost it but you miss tons of combinaitions...

Saige code seems to be perfect, but, im just wondering if the number of code lines or a bit overkill to do what Iwanted....

can we just enhance Fernando script or I need 1 milions lines to do this? lol
0

LVL 41

Expert Comment

ID: 41893519
0

LVL 1

Author Comment

ID: 41893523
im looking now ;)
0

LVL 1

Author Comment

ID: 41893525
could we just Translate in Vb.net ?? or C# ??     :/
0

LVL 41

Expert Comment

ID: 41893530
That is C# . . . it's just a console application.
0

LVL 1

Author Comment

ID: 41893537
oups you're right.
0

LVL 64

Accepted Solution

Fernando Soto earned 1000 total points
ID: 41894969
Hi PhilippeRenaud;

NuGet has a package called Combinatorics Library for .NET which has what you are looking for. If you install that library then all you need to do is something like the following.
``````using Combinatorics.Collections;

// List of characters to be used in the resulting list
char[] alphanumeric = "abcdefghijklmnopqrstuvwxyz01234567.89".ToCharArray();
// Create a collection of 4 chars in each value using above list
Combinations<char> combos = new Combinations<char>(alphanumeric, 4, GenerateOption.WithRepetition);
// Show each value in the collection
foreach (IList<char> chars in combos)
{
// Converts the Combinations<char> to a string value
Console.WriteLine(new string(chars.ToArray()));
}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:â€¦
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediafâ€¦
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month14 days, 15 hours left to enroll