structure toupper\lower?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • didgerman

    structure toupper\lower?

    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.



  • Ben Pfaff

    #2
    Re: structure toupper\lower?

    "didgerman" <aw990012@hotma il.com> writes:
    [color=blue]
    > I need to properly format the case of a struct.[/color]

    Letters have case. Structures don't.
    [color=blue]
    > 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?[/color]

    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.
    [color=blue]
    > The struct will contain some integers, will tolower/upper
    > affect any integers?[/color]

    You should only apply those functions to character data.
    [color=blue]
    > Also...has anyone written an easy struct to xml converter yet?[/color]

    Maybe. A newsgroup that discusses XML tools would probably be a
    better place to ask.
    --
    "...deficie nt support can be a virtue.
    It keeps the amateurs off."
    --Bjarne Stroustrup

    Comment

    • didgerman

      #3
      Re: structure toupper\lower?


      "Ben Pfaff" <blp@cs.stanfor d.edu> wrote in message
      news:87wu7s7loy .fsf@pfaff.stan ford.edu...[color=blue]
      > "didgerman" <aw990012@hotma il.com> writes:
      >[color=green]
      > > I need to properly format the case of a struct.[/color]
      >
      > Letters have case. Structures don't.
      >[color=green]
      > > 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?[/color]
      >
      > 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.
      >[color=green]
      > > The struct will contain some integers, will tolower/upper
      > > affect any integers?[/color]
      >
      > You should only apply those functions to character data.
      >[color=green]
      > > Also...has anyone written an easy struct to xml converter yet?[/color]
      >
      > Maybe. A newsgroup that discusses XML tools would probably be a
      > better place to ask.
      > --
      > "...deficie nt support can be a virtue.
      > It keeps the amateurs off."
      > --Bjarne Stroustrup[/color]

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


      Comment

      • didgerman

        #4
        Re: structure toupper\lower?


        "didgerman" <aw990012@hotma il.com> wrote in message
        news:ZxGNb.4262 $JL4.31811@news fep4-glfd.server.ntl i.net...[color=blue]
        > Chaps,
        > I need to properly format the case of a struct. Can I just hit it[/color]
        with[color=blue]
        > 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.
        >
        >
        >[/color]

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


        Comment

        • CBFalconer

          #5
          Re: structure toupper\lower?

          didgerman wrote:[color=blue]
          >[/color]
          .... snip ...[color=blue]
          >
          > Right, getting somewhere here.
          > How can I use a for loop on a struct? Without using all the
          > struct members.....[/color]

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

          --
          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net> USE worldnet address!

          Comment

          • pete

            #6
            Re: structure toupper\lower?

            CBFalconer wrote:[color=blue]
            >
            > didgerman wrote:[color=green]
            > >[/color]
            > ... snip ...[color=green]
            > >
            > > Right, getting somewhere here.
            > > How can I use a for loop on a struct? Without using all the
            > > struct members.....[/color]
            >
            > There is an interesting "mental" process going on here. I don't
            > think it can be ascribed to a language barrier.[/color]

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

            --
            pete

            Comment

            • Alan Balmer

              #7
              Re: structure toupper\lower?

              On Fri, 16 Jan 2004 09:45:06 -0000, "didgerman" <aw990012@hotma il.com>
              wrote:
              [color=blue]
              >
              >"didgerman" <aw990012@hotma il.com> wrote in message
              >news:ZxGNb.426 2$JL4.31811@new sfep4-glfd.server.ntl i.net...[color=green]
              >> Chaps,
              >> I need to properly format the case of a struct. Can I just hit it[/color]
              >with[color=green]
              >> 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?[/color][/color]

              Only if there is a meaningful mapping. Otherwise, the integer remains
              unchanged.[color=blue][color=green]
              >>
              >> Also...has anyone written an easy struct to xml converter yet?
              >> Cheers for any help.
              >>
              >>
              >>[/color]
              >
              >Right, getting somewhere here.
              >How can I use a for loop on a struct? Without using all the struct
              >members.....
              >Cheers
              >[/color]
              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.

              --
              Al Balmer
              Balmer Consulting
              removebalmercon sultingthis@att .net

              Comment

              • didgerman

                #8
                Re: structure toupper\lower?


                "Alan Balmer" <albalmer@att.n et> wrote in message
                news:ki0g00petn 9cbk9jb8kriv81n 6q0hvjsqs@4ax.c om...[color=blue]
                > On Fri, 16 Jan 2004 09:45:06 -0000, "didgerman"[/color]
                <aw990012@hotma il.com>[color=blue]
                > wrote:
                >[color=green]
                > >
                > >"didgerman" <aw990012@hotma il.com> wrote in message
                > >news:ZxGNb.426 2$JL4.31811@new sfep4-glfd.server.ntl i.net...[color=darkred]
                > >> Chaps,
                > >> I need to properly format the case of a struct. Can I just hit it[/color]
                > >with[color=darkred]
                > >> tolower, and then 'while (string [pos]==' ')
                > >> pos++;
                > >> string[pos]=toupper(string[pos]); to add in the higher case for[/color][/color][/color]
                the[color=blue][color=green][color=darkred]
                > >> start of each letter?
                > >> The struct will contain some integers, will tolower/upper affect[/color][/color][/color]
                any[color=blue][color=green][color=darkred]
                > >> integers?[/color][/color]
                >
                > Only if there is a meaningful mapping. Otherwise, the integer[/color]
                remains[color=blue]
                > unchanged.[color=green][color=darkred]
                > >>
                > >> Also...has anyone written an easy struct to xml converter yet?
                > >> Cheers for any help.
                > >>
                > >>
                > >>[/color]
                > >
                > >Right, getting somewhere here.
                > >How can I use a for loop on a struct? Without using all the struct
                > >members.....
                > >Cheers
                > >[/color]
                > 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.
                >
                > --
                > Al Balmer
                > Balmer Consulting
                > removebalmercon sultingthis@att .net[/color]

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


                Comment

                • didgerman

                  #9
                  Re: structure toupper\lower?


                  "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                  news:4007F4E5.9 B82BEF2@yahoo.c om...[color=blue]
                  > didgerman wrote:[color=green]
                  > >[/color]
                  > ... snip ...[color=green]
                  > >
                  > > Right, getting somewhere here.
                  > > How can I use a for loop on a struct? Without using all the
                  > > struct members.....[/color]
                  >
                  > There is an interesting "mental" process going on here. I don't
                  > think it can be ascribed to a language barrier.
                  >
                  > --
                  > Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                  > Available for consulting/temporary embedded and systems.
                  > <http://cbfalconer.home .att.net> USE worldnet address!
                  >[/color]

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


                  Comment

                  • Keith Thompson

                    #10
                    Re: structure toupper\lower?

                    "didgerman" <aw990012@hotma il.com> writes:[color=blue]
                    > "didgerman" <aw990012@hotma il.com> wrote in message
                    > news:ZxGNb.4262 $JL4.31811@news fep4-glfd.server.ntl i.net...[color=green]
                    > > 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?[/color][/color]
                    [...][color=blue]
                    >
                    > Right, getting somewhere here.
                    > How can I use a for loop on a struct? Without using all the struct
                    > members.....[/color]

                    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[i] != '\0'; i ++) {
                    s[i] = toupper(s[i]);
                    }

                    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_VALU E];
                    char c;
                    } my_struct_objec t;

                    map_string_to_u pper(my_struct_ object.name);
                    map_string_to_u pper(my_struct_ object.str);
                    my_struct_objec t.c = toupper(my_stru ct_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?

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
                    Schroedinger does Shakespeare: "To be *and* not to be"

                    Comment

                    • CBFalconer

                      #11
                      Re: structure toupper\lower?

                      didgerman wrote:[color=blue]
                      > "CBFalconer " <cbfalconer@yah oo.com> wrote in message[color=green]
                      > > didgerman wrote:[color=darkred]
                      > > >[/color]
                      > > ... snip ...[color=darkred]
                      > > >
                      > > > Right, getting somewhere here.
                      > > > How can I use a for loop on a struct? Without using all the
                      > > > struct members.....[/color]
                      > >
                      > > There is an interesting "mental" process going on here. I don't
                      > > think it can be ascribed to a language barrier.[/color]
                      >
                      > I'm sure that means something to you mate, well done.[/color]

                      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.

                      --
                      Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                      Available for consulting/temporary embedded and systems.
                      <http://cbfalconer.home .att.net> USE worldnet address!


                      Comment

                      • didgerman

                        #12
                        Re: structure toupper\lower?


                        "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                        news:40084C05.B 133D6EC@yahoo.c om...[color=blue]
                        > didgerman wrote:[color=green]
                        > > "CBFalconer " <cbfalconer@yah oo.com> wrote in message[color=darkred]
                        > > > didgerman wrote:
                        > > > >
                        > > > ... 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.[/color]
                        > >
                        > > I'm sure that means something to you mate, well done.[/color]
                        >
                        > 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.
                        >
                        > --
                        > Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                        > Available for consulting/temporary embedded and systems.
                        > <http://cbfalconer.home .att.net> USE worldnet address!
                        >
                        >[/color]

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


                        Comment

                        • didgerman

                          #13
                          Re: structure toupper\lower?


                          "Keith Thompson" <kst-u@mib.org> wrote in message
                          news:lnad4ntoxq .fsf@nuthaus.mi b.org...[color=blue]
                          > "didgerman" <aw990012@hotma il.com> writes:[color=green]
                          > > "didgerman" <aw990012@hotma il.com> wrote in message
                          > > news:ZxGNb.4262 $JL4.31811@news fep4-glfd.server.ntl i.net...[color=darkred]
                          > > > Chaps,
                          > > > I need to properly format the case of a struct. Can I just hit[/color][/color][/color]
                          it with[color=blue][color=green][color=darkred]
                          > > > tolower, and then 'while (string [pos]==' ')
                          > > > pos++;
                          > > > string[pos]=toupper(string[pos]); to add in the higher case for[/color][/color][/color]
                          the[color=blue][color=green][color=darkred]
                          > > > start of each letter?
                          > > > The struct will contain some integers, will tolower/upper affect[/color][/color][/color]
                          any[color=blue][color=green][color=darkred]
                          > > > integers?[/color][/color]
                          > [...][color=green]
                          > >
                          > > Right, getting somewhere here.
                          > > How can I use a for loop on a struct? Without using all the struct
                          > > members.....[/color]
                          >
                          > 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[/color]
                          nail.[color=blue]
                          >
                          > 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[/color]
                          iterate[color=blue]
                          > 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[/color]
                          pointer[color=blue]
                          > arithmetic:
                          >
                          > char *s = "hello, world";
                          > int i;
                          > for (i = 0; s[i] != '\0'; i ++) {
                          > s[i] = toupper(s[i]);
                          > }
                          >
                          > You can encapsulate the loop by putting it into a function that[/color]
                          takes[color=blue]
                          > 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_VALU E];
                          > char c;
                          > } my_struct_objec t;
                          >
                          > map_string_to_u pper(my_struct_ object.name);
                          > map_string_to_u pper(my_struct_ object.str);
                          > my_struct_objec t.c = toupper(my_stru ct_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,[/color]
                          or[color=blue]
                          > just up to a terminating '\0' character?
                          >
                          > --
                          > Keith Thompson (The_Other_Keit h) kst-u@mib.org[/color]
                          <http://www.ghoti.net/~kst>[color=blue]
                          > San Diego Supercomputer Center <*>[/color]
                          <http://www.sdsc.edu/~kst>[color=blue]
                          > Schroedinger does Shakespeare: "To be *and* not to be"[/color]

                          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....... .


                          Comment

                          • Ben Pfaff

                            #14
                            Re: structure toupper\lower?

                            "didgerman" <aw990012@hotma il.com> writes:
                            [color=blue]
                            > What I want to do is loop through a struct without specifying all the
                            > members.
                            > A for loop would suit me best.[/color]

                            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.
                            --
                            int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                            \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                            );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                            );}return 0;}

                            Comment

                            • CBFalconer

                              #15
                              Re: structure toupper\lower?

                              didgerman wrote:[color=blue]
                              >[/color]
                              .... snip ...[color=blue]
                              >
                              > I've got char' arrays, nul terminated iterating over the 'isalpha'
                              > chars only would be best, in case the code develops further.[/color]

                              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_FAILU RE);
                              }

                              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(astri ngs[i]);
                              }

                              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.

                              --
                              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net> USE worldnet address!


                              Comment

                              Working...