Simple C Question

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
Is there any simple way to cycle through all characters A-Z, a-z, 0-9 without doing each set separately?

Currently my code looks like this:

char c
for (c = 'A'; c <= 'Z'; c++)

but I'd like to have c loop through the sets a-z and 0-9 as well. Thanks.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Ascii values. I'd suggest your could probably loop using a byte then cast the byte to a character.

Edit: Scratch that. There are control characters in between them.

Edit 2: Well, maybe not. You'd need another line though.

char c
for (c='0';c<='z'; c++)
{
if isalnum(c)
{
do something
}
}


C isn't my language though, so I'm 100% sure on that.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Option 1:

Create a fixed array containing the combiniations of characters that you stated - This will take 63 bytes. (including the termination NULL)

Option 2:
Loop from 0->9;

Have two loops (nested or seperate) for a-z;
Convert the a-z character to upper for the second loop.

This may result in less code, but is uglier and more time consuming.

Option 3:
Loop from 0->9;

Have one loop for a-z;
Convert the a-z character to upper for the second test.

Again, this may result in less code, but is uglier and more time consuming.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Lowercase and uppercase can be done together by simply adding the difference (32). So you loop one of them and depending on how many ops reference the other set, either hardcode the math (char + 32) or create a variable.

As for numbers, I'd recommend probably doing them separately. No sense making something harder to try to make it smaller.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Or

I belive that this is the proper syntax to increment a pointer

 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: Ryland
You forgot the -1 since C is 0 based indices.

You're forgetting that C++ assigning of strings creates a null-terminated string. Therefore the second loop would include the null value. For example:

char string = "123" is actually 1,2,3,\0

The length of this includes the null terminator, since it is a character. So the length reported by sizeof is 4 (each char being a byte). referencing everything less than the sizeof (i.e. 4) will go through 0 through 3 which means you go through 1, 2, 3 and finally \0. Depending on what the action being taken in this loop is, this may not even matter.

The first one will work, because the sizeof - 1 will point to the \0, therefore, once the loop sees the null terminator, it will stop.
 

Ryland

Platinum Member
Aug 9, 2001
2,810
13
81
Originally posted by: HigherGround
I didn't forget about anything. Loop terminates on '\0'.

Actually if it is C and you haven't specified that the array ends in '\0' then the character following 'Z' won't necessarily be '\0'.

Is the OP trying to do C or C++ (thought it was C).
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Actually if it is C and you haven't specified that the array ends in '\0' then the character following 'Z' won't necessarily be '\0'.

ANSI C null-terminates string literals.

Although maybe there's some weird behavior around declaring a 'char[ ]' as opposed to a 'char *'? I really thought they worked the same way...
 

Ryland

Platinum Member
Aug 9, 2001
2,810
13
81
Originally posted by: Matthias99

ANSI C null-terminates string literals.

Although maybe there's some weird behavior around declaring a 'char[ ]' as opposed to a 'char *'? I really thought they worked the same way...


char* and char[] are two different beasts. Im pretty sure you are correct that a string literal would be null terminated but a char array would only contain what you specified was in it.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
This not about string literals or whatever else you might have had imagined. This is strictly about static storage initializers.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Defining a character string that is 'bound' by the double quotes initially generates a Null terminator per the ANSI spec.

The Null termiantor can be overwritten by code execution or assignments
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: Ryland
Originally posted by: Matthias99

ANSI C null-terminates string literals.

Although maybe there's some weird behavior around declaring a 'char[ ]' as opposed to a 'char *'? I really thought they worked the same way...

char* and char[] are two different beasts. Im pretty sure you are correct that a string literal would be null terminated but a char array would only contain what you specified was in it.

...but you initialized it with a string literal (which should be null-terminated)!

this:

char myArray [ ] = {'a', 'b', 'c'};

produces a non-null-terminated array of character values.

but I thought this:

char myArray [ ] = "abc";

or this:

char * myString = "abc";

produces a null-terminated string. I'll test it, but I'm 99% sure it works this way.

Edit: tested it. 'char myArray [ ] = "abc";' produces a null-terminated string in myArray.
 

Ryland

Platinum Member
Aug 9, 2001
2,810
13
81
Ok, the compiler must be interpreting it as a char * instead of char[] or maybe the string is what is doing it. I wouldn't trust every compiler to act the same way though...
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: Ryland
Ok, the compiler must be interpreting it as a char * instead of char[] or maybe the string is what is doing it. I wouldn't trust every compiler to act the same way though...

I am nearly certain that ANSI C requires this behavior (I'll see if I can find a more explicit reference stating this.)

If you think it is allowed to behave otherwise I'd love to see a source...

Edit: references. ANSI C requires this, and any compiler that doesn't do this is screwing up.

From SGI's reference book :

String Literals

A string literal is a sequence of characters surrounded by double quotation marks, as in "...". A string literal has type array of char and is initialized with the given characters. The compiler places a null byte (\0) at the end of each string literal so that programs that scan the string literal can find its end. A double-quotation character (") in a string literal must be preceded by a backslash (\). In addition, the same escapes as those described for character constants can be used. (See ?Character Constants?, for a list of escapes.) A backslash (\) and the immediately following newline are ignored. Adjacent string literals are concatenated.

In traditional C, all string literals, even when written identically, are distinct.

In ANSI C, identical string literals are not necessarily distinct. Prefixing a string literal with L specifies a wide string literal. Adjacent wide string literals are concatenated.

(emphasis added)

From another page of their documentation

(under "Initialization"):

...A final abbreviation allows a char array to be initialized by a string literal. In this case, successive characters of the string literal initialize the members of the array.

...

The final example shows a character array whose members are initialized with a string literal. The length of the string (or size of the array) includes the terminating NULL character, \0:

char msg[] = "Syntax error on line %s\n";

(emphasis added)