A Newbie Increment Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,
I was wondering if there is an easy way to increment a char[].
eg. char[] c = {z,x} would increment to {z,y}, {z,z}, {0,0,0}, {0,0,1} and
so on.
Is this easy or should I be using another type to do this instead of a char[]?

Thanks in advance for your help.
 
Tim said:
I was wondering if there is an easy way to increment a char[].
eg. char[] c = {z,x} would increment to {z,y}, {z,z}, {0,0,0}, {0,0,1} and
so on.
Is this easy or should I be using another type to do this instead of a char[]?

Thanks in advance for your help.

It's not clear what you mean, I'm afraid. Could you give a more
concrete example?
 
Tim said:
I was wondering if there is an easy way to
increment a char[]. eg. char[] c = {z,x} would
increment to {z,y}, {z,z}, {0,0,0}, {0,0,1} and
so on.

Your incrementing does not follow logical rules. '0' is not the
character that follows 'z'; in fact, it comes somewhat *before* all of
the alphabetic characters.

You could try something like this:

char[] c = { 'a', 'b', 'c' };
c[c.Length - 1] = (char) ( 1 + (int) c[c.Length - 1] );

But you'll need to check specifically for the case where you "run out
of characters" and then add the next one to the array and reset the
others. So I suggest you write your own class that supports this
operation in itself.

P.
 
if you have :
char c = 'a';
than doing :
c = (char)( ((int)c) + 1);
will have a 'b' in char c.

I hope that helps.


Very unneccesary casting here...

c++;

...is enough to increment 'a' to 'b'.


// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top