structure toupper\lower?

D

didgerman

Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?

Also...has anyone written an easy struct to xml converter yet?
Cheers for any help.
 
B

Ben Pfaff

didgerman said:
I need to properly format the case of a struct.

Letters have case. Structures don't.
Can I just hit it with tolower, and then 'while (string
[pos]==' ') pos++; string[pos]=toupper(string[pos]); to add in
the higher case for the start of each letter?

You can certainly do something like that for the character
strings in the structure. Remember to cast the argument of
toupper() and tolower() to `unsigned char', though, for greatest
portability.
The struct will contain some integers, will tolower/upper
affect any integers?

You should only apply those functions to character data.
Also...has anyone written an easy struct to xml converter yet?

Maybe. A newsgroup that discusses XML tools would probably be a
better place to ask.
 
D

didgerman

Ben Pfaff said:
didgerman said:
I need to properly format the case of a struct.

Letters have case. Structures don't.
Can I just hit it with tolower, and then 'while (string
[pos]==' ') pos++; string[pos]=toupper(string[pos]); to add in
the higher case for the start of each letter?

You can certainly do something like that for the character
strings in the structure. Remember to cast the argument of
toupper() and tolower() to `unsigned char', though, for greatest
portability.
The struct will contain some integers, will tolower/upper
affect any integers?

You should only apply those functions to character data.
Also...has anyone written an easy struct to xml converter yet?

Maybe. A newsgroup that discusses XML tools would probably be a
better place to ask.

Thnx for that.
As for C to xml, I'd have thought either ng tbh.....
 
D

didgerman

didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?

Also...has anyone written an easy struct to xml converter yet?
Cheers for any help.

Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....
Cheers
 
C

CBFalconer

didgerman said:
.... snip ...

Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the
struct members.....

There is an interesting "mental" process going on here. I don't
think it can be ascribed to a language barrier.
 
P

pete

CBFalconer said:
There is an interesting "mental" process going on here. I don't
think it can be ascribed to a language barrier.

I think he's talking about using an array of offsets.
I've never used the offsetof() macro from stddef.h
 
A

Alan Balmer

didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?

Only if there is a meaningful mapping. Otherwise, the integer remains
unchanged.
Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....
Cheers
Hmmm... I think you had better post an actual example of what you're
trying to do. I think there may be some misconceptions here, and
they're probably going to get worse without a concrete example.
 
D

didgerman

Alan Balmer said:
didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?

Only if there is a meaningful mapping. Otherwise, the integer remains
unchanged.

Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....
Cheers
Hmmm... I think you had better post an actual example of what you're
trying to do. I think there may be some misconceptions here, and
they're probably going to get worse without a concrete example.

I've gone back to strings for now.
It's Friday and I can't be arsed.
thnx anyway.
 
D

didgerman

CBFalconer said:
There is an interesting "mental" process going on here. I don't
think it can be ascribed to a language barrier.

I'm sure that means something to you mate, well done.
 
K

Keith Thompson

didgerman said:
didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?
[...]

Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....

That's sort of like asking how you can drive a nail using a
screwdriver. You should probably just be asking how to drive a nail.

Are you trying to iterate over the members of a struct? You can't.
(Actually, you probably can if you first build an array each element
of which contains offset and size information for the struct members
you're interested in, but that's almost certainly more effort than
it's worth.)

The toupper() and tolower() functions apply to a single character:

char c = some_value;
c = toupper(c);

To map a string to upper or lower case, you can use a loop to iterate
over the characters of the array:

char *s = "hello, world";
char *ptr;
for (ptr = s; *ptr != '\0'; ptr ++) {
*ptr = toupper(*ptr);
}

or, if you're more comfortable with array indexing rather than pointer
arithmetic:

char *s = "hello, world";
int i;
for (i = 0; s != '\0'; i ++) {
s = toupper(s);
}

You can encapsulate the loop by putting it into a function that takes
a pointer to a string and maps the string to upper case.

If you have a struct some of whose members are character arrays
containing string values, and you want to map each such member to
upper case, the best approach is just to explicitly map each member:

struct my_struct_type {
int x;
char name[MAX_NAME_LEN];
int y;
char str[SOME_OTHER_VALUE];
char c;
} my_struct_object;

map_string_to_upper(my_struct_object.name);
map_string_to_upper(my_struct_object.str);
my_struct_object.c = toupper(my_struct_object.c);

There are still a lot of possible complications. Are the members
you're dealing with character arrays or character pointers? If
they're arrays, are they nul-terminated strings or just arbitrary
arrays of characters; do you want to iterate over the entire array, or
just up to a terminating '\0' character?
 
C

CBFalconer

didgerman said:
I'm sure that means something to you mate, well done.

Your questions and statements make absolutely no sense in the
context of C programming. You seem to have some very peculiar
ideas about it. For example, a for loop is a control mechanism.
It is not something that is applied to something else.
 
D

didgerman

CBFalconer said:
Your questions and statements make absolutely no sense in the
context of C programming. You seem to have some very peculiar
ideas about it. For example, a for loop is a control mechanism.
It is not something that is applied to something else.

What I want to do is loop through a struct without specifying all the
members.
A for loop would suit me best.
 
D

didgerman

Keith Thompson said:
didgerman said:
didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?
[...]

Right, getting somewhere here.
How can I use a for loop on a struct? Without using all the struct
members.....

That's sort of like asking how you can drive a nail using a
screwdriver. You should probably just be asking how to drive a nail.

Are you trying to iterate over the members of a struct? You can't.
(Actually, you probably can if you first build an array each element
of which contains offset and size information for the struct members
you're interested in, but that's almost certainly more effort than
it's worth.)

The toupper() and tolower() functions apply to a single character:

char c = some_value;
c = toupper(c);

To map a string to upper or lower case, you can use a loop to iterate
over the characters of the array:

char *s = "hello, world";
char *ptr;
for (ptr = s; *ptr != '\0'; ptr ++) {
*ptr = toupper(*ptr);
}

or, if you're more comfortable with array indexing rather than pointer
arithmetic:

char *s = "hello, world";
int i;
for (i = 0; s != '\0'; i ++) {
s = toupper(s);
}

You can encapsulate the loop by putting it into a function that takes
a pointer to a string and maps the string to upper case.

If you have a struct some of whose members are character arrays
containing string values, and you want to map each such member to
upper case, the best approach is just to explicitly map each member:

struct my_struct_type {
int x;
char name[MAX_NAME_LEN];
int y;
char str[SOME_OTHER_VALUE];
char c;
} my_struct_object;

map_string_to_upper(my_struct_object.name);
map_string_to_upper(my_struct_object.str);
my_struct_object.c = toupper(my_struct_object.c);

There are still a lot of possible complications. Are the members
you're dealing with character arrays or character pointers? If
they're arrays, are they nul-terminated strings or just arbitrary
arrays of characters; do you want to iterate over the entire array, or
just up to a terminating '\0' character?

San Diego Supercomputer Center <*>
Schroedinger does Shakespeare: "To be *and* not to be"

Thnx mate, got it in one there.
I've got char' arrays, nul terminated iterating over the 'isalpha'
chars only would be best, in case the code develops further.
Unlikely........
 
B

Ben Pfaff

didgerman said:
What I want to do is loop through a struct without specifying all the
members.
A for loop would suit me best.

This isn't something that can be done in a natural way in C.
Maybe you should describe your actual situation in more detail,
and then perhaps we can give you a better way to cast that into C
terms.
 
C

CBFalconer

didgerman said:
.... snip ...

I've got char' arrays, nul terminated iterating over the 'isalpha'
chars only would be best, in case the code develops further.

Then you are probably using the wrong data structure. Consider
something like:

/* if at file level this will be auto-initialized to NULLs */
char *astrings[MAXCOUNT];

void insert(int where, const char *s)
{
if (astrings[where] = malloc(1 + strlen(s)))
strcpy(astrings[where], s);
else
exit(EXIT_FAILURE);
}

which you can use to install modifiable strings into the astrings
array. You might have something like:

insert(0, "string 0");
insert(1, "string 1");

and so forth. Now, you can scan through them all with:

for (i = 0; i < MAXCOUNT; i++)
operateon(astrings);
}

and operateon should guard against NULL and look something like:

void operateon(char *s)
{
if (s) {
/* whatever code you need, s is non-NULL */
}
}

All this assumes we have understood what you are trying to do,
which you did not describe very well. Don't forget the
appropriate #includes, which I have not specified above. If I am
right about your needs, ensure you understand why I wrote each and
every code line above.
 
H

Hallvard B Furuseth

Keith said:
char c = some_value;
c = toupper(c);

That can crash if 'char' is signed c becomes negative (i.e. for
non-ASCII characters if you have 8-bit bytes and an ASCII superset).
It should be

c = toupper((unsigned char) c);
 
S

Severian

I think he's talking about using an array of offsets.
I've never used the offsetof() macro from stddef.h

I've found offsetof() useful for building portable, efficient,
table-driven, readable and easy-to-maintain access to binary files (of
external definition). I define tables that map "natural" C structures
to the underlying, unaligned binary data, and handle endianness (sic?)
implicitly.

Thus, C-aligned, native-endian structures can be read and written from
any-endian binary files with simple calls.

The OP could do something similar to point to the strings in the
structs, but unless he's got dozens of structs to do this with, it's
probably not worth the effort.

- Sev
 
K

Keith Thompson

Hallvard B Furuseth said:
That can crash if 'char' is signed c becomes negative (i.e. for
non-ASCII characters if you have 8-bit bytes and an ASCII superset).
It should be

c = toupper((unsigned char) c);

Oops, you're right. Thanks.
 
D

didgerman

didgerman said:
Chaps,
I need to properly format the case of a struct. Can I just hit it with
tolower, and then 'while (string [pos]==' ')
pos++;
string[pos]=toupper(string[pos]); to add in the higher case for the
start of each letter?
The struct will contain some integers, will tolower/upper affect any
integers?

Also...has anyone written an easy struct to xml converter yet?
Cheers for any help.

Chaps, I'm done, just about.
Thnx for all the help.
I'll continue to lurk here and pick a few things up.
Cheers.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top