Solved

# Filling an array of characters....

Posted on 1999-11-25
Medium Priority
263 Views
I have problems declaring and filling an array of characters.

By the way: I want to fill that array with ASCII Codes (i.e. a-z and A-Z) and therefore thought of something like:

for (i;i=<52;i++) {
//fill array[i]=asc(i) <-- I know asc() is from basic. what is the //counterpart in C
}

Thanks

0
Question by:Stapman

LVL 3

Accepted Solution

danny_pav earned 200 total points
ID: 2234375
cycle through them using char

char val = 'a';

++val;

now val holds 'b'

cycle all the way to 'z'

same with
val = 'A' + 4;
val now holds 'E';

etc.

this should get you going in the right direction.
0

Author Comment

ID: 2234537
Ok. That should do the thing concerning the information I want to fill into the array. But how do I do that (declaration of array of characters and filling that array with the information).

Thanks
0

LVL 7

Expert Comment

ID: 2234546
In C/C++ the integer value of a char IS the ASCII value. Where in basic you would write:
dim i as integer
dim c as character ' Made that up?
i = asc("a")
c = chr(i)
in C this becomes,
int i;
char c;
i = 'a';
c = i; // c = (char)(i)
0

LVL 9

Expert Comment

ID: 2234773
so to fill a..zA..Z your code becomes...

char array[52];
for (int i=0;i<52;i++) {
array[i] = (i<26)?'a'+i:'A'+(i-26);
}
0

LVL 3

Expert Comment

ID: 2235042
declare you array:

type name[const_expression_count];

so it becomes

char a[52];

for (initializer; loop_continue_check; incrementer)
statement;

so it becomes
for (int i = 0, char fill = 'a'; fill <= 'Z'; ++i, ++fill)
{
if (fill == ('z' + 1))
fill = 'A';
a[i] = fill;
}
0

Author Comment

ID: 2236108
Thanks
0

## Featured Post

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month5 days, 5 hours left to enroll