?
Solved

Filling an array of characters....

Posted on 1999-11-25
6
Medium Priority
?
263 Views
Last Modified: 2010-04-01
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
Comment
Question by:Stapman
6 Comments
 
LVL 3

Accepted Solution

by:
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

by:Stapman
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

by:KangaRoo
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 9

Expert Comment

by:jasonclarke
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

by:danny_pav
ID: 2235042
declare you array:

type name[const_expression_count];

so it becomes

char a[52];

do your for loop:
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

by:Stapman
ID: 2236108
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question