lower case to upper case

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

    #1

    lower case to upper case

    char* line = "abcd";
    How to convert the line to upper case and print?
    Any option for printf to do this?
    Thanx


  • Ravi Uday

    #2
    Re: lower case to upper case



    Janice wrote:[color=blue]
    > char* line = "abcd";
    > How to convert the line to upper case and print?
    > Any option for printf to do this?
    > Thanx
    >
    >[/color]

    Not sure if there is a exclusive function in 'C' to do it
    but tivially you could do:

    - Get each character one by one from the input array/string.
    - Check whether the character is lower/upper using 'islower'or 'isupper'
    functions
    - If character is upper case, then Add 32 to get its lower case
    equivalent or
    - If character is lower case Subtract 32 to get its upper case.
    - Store or print the converted characters.

    You might want to do error checking on the input array too if they are
    valid alphabets !

    - Ravi

    Comment

    • Joona I Palaste

      #3
      Re: lower case to upper case

      Ravi Uday <raviuday@gmail .com> scribbled the following:[color=blue]
      > Janice wrote:[color=green]
      >> char* line = "abcd";
      >> How to convert the line to upper case and print?
      >> Any option for printf to do this?
      >> Thanx[/color][/color]
      [color=blue]
      > Not sure if there is a exclusive function in 'C' to do it[/color]

      There is. Check out toupper().
      [color=blue]
      > but tivially you could do:[/color]
      [color=blue]
      > - Get each character one by one from the input array/string.
      > - Check whether the character is lower/upper using 'islower'or 'isupper'
      > functions
      > - If character is upper case, then Add 32 to get its lower case
      > equivalent or
      > - If character is lower case Subtract 32 to get its upper case.
      > - Store or print the converted characters.[/color]

      Bzzzt. No one told you you were using ASCII, ISO-8859-1 or any other
      charset where upper and lower case are 32 bytes apart.

      --
      /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
      \-------------------------------------------------------- rules! --------/
      "Stronger, no. More seductive, cunning, crunchier the Dark Side is."
      - Mika P. Nieminen

      Comment

      • Zoran Cutura

        #4
        Re: lower case to upper case

        Ravi Uday <raviuday@gmail .com> wrote:[color=blue]
        >
        >
        > Janice wrote:[color=green]
        >> char* line = "abcd";
        >> How to convert the line to upper case and print?
        >> Any option for printf to do this?
        >> Thanx
        >>
        >>[/color]
        >
        > Not sure if there is a exclusive function in 'C' to do it
        > but tivially you could do:
        >
        > - Get each character one by one from the input array/string.
        > - Check whether the character is lower/upper using 'islower'or 'isupper'
        > functions
        > - If character is upper case, then Add 32 to get its lower case
        > equivalent or
        > - If character is lower case Subtract 32 to get its upper case.
        > - Store or print the converted characters.
        >
        > You might want to do error checking on the input array too if they are
        > valid alphabets !
        >[/color]


        ever heard of tolower/toupper?

        Your suggestions are not portable and will only function on a machine
        where all lower case characters are represented by a value that is by 32
        bigger than the according upper case characters representation. As this
        need not actually be the case the standard provides you with functions
        that need to be supported by your implementations , which makes usage of
        them portable.

        --
        Z (Zoran.Cutura@d aimlerchrysler. com)
        "LISP is worth learning for the profound enlightenment experience
        you will have when you finally get it; that experience will make you
        a better programmer for the rest of your days." -- Eric S. Raymond

        Comment

        • Ravi Uday

          #5
          Re: lower case to upper case



          Joona I Palaste wrote:[color=blue]
          > Ravi Uday <raviuday@gmail .com> scribbled the following:
          >[color=green]
          >>Janice wrote:
          >>[color=darkred]
          >>>char* line = "abcd";
          >>>How to convert the line to upper case and print?
          >>>Any option for printf to do this?
          >>>Thanx[/color]
          >>[/color]
          >[color=green]
          >>Not sure if there is a exclusive function in 'C' to do it[/color]
          >
          >
          > There is. Check out toupper().[/color]
          Yes it is, missed it some how, Thanks.[color=blue]
          >
          >[color=green]
          >>but tivially you could do:[/color]
          >
          >[color=green]
          >>- Get each character one by one from the input array/string.
          >>- Check whether the character is lower/upper using 'islower'or 'isupper'
          >> functions
          >>- If character is upper case, then Add 32 to get its lower case
          >> equivalent or
          >>- If character is lower case Subtract 32 to get its upper case.
          >>- Store or print the converted characters.[/color][/color]

          To O.P.: the above works for ASCII character set only :-)[color=blue]
          >
          >
          > Bzzzt. No one told you you were using ASCII, ISO-8859-1 or any other
          > charset where upper and lower case are 32 bytes apart.
          >[/color]

          Comment

          • CBFalconer

            #6
            Re: lower case to upper case

            Janice wrote:[color=blue]
            >
            > char* line = "abcd";
            > How to convert the line to upper case and print?
            > Any option for printf to do this?[/color]

            You can't and no. However if you had defined line as:

            char line[] = "abcd";

            you would have been able to convert it. Now you should spend some
            time thinking about what the difference is. It is fundamental.

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

            • Mark A. Odell

              #7
              Re: lower case to upper case

              "Janice" <no@mail.com> wrote in news:cpbs8q$8q9 1@imsp212.netvi gator.com:
              [color=blue]
              > char* line = "abcd";[/color]

              Pointer mis-match, char *line should not point to a const char * "abcd".
              What if "abcd" is placed into non-writable memory?
              [color=blue]
              > How to convert the line to upper case and print?[/color]

              Use a writable array, like CBFalconer suggests, then call toupper() on the
              array.
              [color=blue]
              > Any option for printf to do this?[/color]

              printf("%s\n", name-of-writable-array);
              [color=blue]
              > Thanx[/color]
              Thanks

              --
              - Mark ->
              --

              Comment

              • Eric Sosman

                #8
                Re: lower case to upper case

                Mark A. Odell wrote:[color=blue]
                > "Janice" <no@mail.com> wrote in news:cpbs8q$8q9 1@imsp212.netvi gator.com:
                >[color=green]
                >>char* line = "abcd";[/color]
                >
                > Pointer mis-match, char *line should not point to a const char * "abcd".
                > What if "abcd" is placed into non-writable memory?[/color]

                No mismatch. The literal creates an array of ordinary
                `char', not `const'-qualified. True, that array cannot be
                written safely, but its type is non-`const' anyhow.
                [color=blue][color=green]
                >>How to convert the line to upper case and print?[/color]
                >
                > Use a writable array, like CBFalconer suggests, then call toupper() on the
                > array.[/color]

                The argument to toupper() is an `int', not an array.
                And there is no need for a writeable array anyhow:

                char *line = "abcd";
                while (*line != '\0')
                putchar (toupper( (unsigned char)*line++ ));

                (The `(unsigned char)' cast guards against the possibility
                that negative-valued characters might appear in the string.
                In this particular example all the characters have positive
                values, but get in the habit of using the cast anyhow.)

                --
                Eric.Sosman@sun .com

                Comment

                • Mark A. Odell

                  #9
                  Re: lower case to upper case

                  Eric Sosman <eric.sosman@su n.com> wrote in
                  news:cpcfdq$s0k $1@news1brm.Cen tral.Sun.COM:
                  [color=blue][color=green][color=darkred]
                  >>>char* line = "abcd";[/color]
                  >>
                  >> Pointer mis-match, char *line should not point to a const char *
                  >> "abcd". What if "abcd" is placed into non-writable memory?[/color]
                  >
                  > No mismatch. The literal creates an array of ordinary
                  > `char', not `const'-qualified. True, that array cannot be
                  > written safely, but its type is non-`const' anyhow.[/color]

                  You're rigth, sadly. What an awful choice. I still find bugs like this
                  where people assume a char * is writable (as one might expect) but where
                  the pointer points to a string literal. Wouldn't it have been better to
                  have string literals be of type const char *?

                  --
                  - Mark ->
                  --

                  Comment

                  • Keith Thompson

                    #10
                    Re: lower case to upper case

                    CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
                    > Janice wrote:[color=green]
                    >>
                    >> char* line = "abcd";
                    >> How to convert the line to upper case and print?
                    >> Any option for printf to do this?[/color]
                    >
                    > You can't and no. However if you had defined line as:
                    >
                    > char line[] = "abcd";
                    >
                    > you would have been able to convert it. Now you should spend some
                    > time thinking about what the difference is. It is fundamental.[/color]

                    That depends on whether "convert" means to convert it in-place, or to
                    create a converted copy. The problem statement doesn't make this
                    clear.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Eric Sosman

                      #11
                      Re: lower case to upper case

                      Mark A. Odell wrote:[color=blue]
                      > Eric Sosman <eric.sosman@su n.com> wrote in
                      > news:cpcfdq$s0k $1@news1brm.Cen tral.Sun.COM:
                      >
                      >[color=green][color=darkred]
                      >>>>char* line = "abcd";
                      >>>
                      >>>Pointer mis-match, char *line should not point to a const char *
                      >>>"abcd". What if "abcd" is placed into non-writable memory?[/color]
                      >>
                      >> No mismatch. The literal creates an array of ordinary
                      >>`char', not `const'-qualified. True, that array cannot be
                      >>written safely, but its type is non-`const' anyhow.[/color]
                      >
                      > You're rigth, sadly. What an awful choice. I still find bugs like this
                      > where people assume a char * is writable (as one might expect) but where
                      > the pointer points to a string literal. Wouldn't it have been better to
                      > have string literals be of type const char *?[/color]

                      According to the Rationale, string literals have this
                      peculiar property to avoid breaking existing code:

                      [...] string literals do not have the type /array
                      of const char/ in order to avoid the problems of
                      pointer type checking, particularly with library
                      functions, since assigning a /pointer to const char/
                      to a plain /pointer to char/ is not valid. [...]

                      As an example of how `const' literals could break
                      perfectly good existing code, consider this pre-C89 program:

                      #include <stdio.h>

                      greet(whom)
                      char *whom;
                      {
                      printf ("Hello, %s!\n", whom);
                      }

                      int main() {
                      greet ("world");
                      return 0;
                      }

                      Note that greet() takes a plain `char*' argument, because
                      a pre-C89 coder had no way to write a `const' qualifier: the
                      keyword was added to the language (along with `void' and some
                      other "new stuff") by the ANSI committee. Now, what would
                      have happened if the committee had made string literals be
                      `const char[]'? The call to greet() in main() would have
                      become illegal, because you can't convert `const char*' to
                      plain `char*' without a cast.

                      Now multiply this trivial example by the twenty or so
                      years' worth of C code in existence before the new Standard
                      came along. If every string-accepting function in the entire
                      corpus of existing code had suddenly started refusing to accept
                      string literals as arguments, the new Standard might have had
                      some slight difficulty in gaining acceptance ...

                      Hindsight often tells us how things ought to have been
                      done differently, but it's not always possible to undo them.

                      The Clacking Keyboard writes: and having writ,
                      Clacks on: nor all thy Prototypes nor Wit
                      Shall rewind() it to fgets() half a Line,
                      Nor all thy Tears re-Const a Char of it.

                      --
                      Eric.Sosman@sun .com

                      Comment

                      • CBFalconer

                        #12
                        Re: lower case to upper case

                        "Mark A. Odell" wrote:[color=blue]
                        > Eric Sosman <eric.sosman@su n.com> wrote in
                        >[color=green][color=darkred]
                        >>>> char* line = "abcd";
                        >>>
                        >>> Pointer mis-match, char *line should not point to a const char *
                        >>> "abcd". What if "abcd" is placed into non-writable memory?[/color]
                        >>
                        >> No mismatch. The literal creates an array of ordinary
                        >> `char', not `const'-qualified. True, that array cannot be
                        >> written safely, but its type is non-`const' anyhow.[/color]
                        >
                        > You're rigth, sadly. What an awful choice. I still find bugs like
                        > this where people assume a char * is writable (as one might expect)
                        > but where the pointer points to a string literal. Wouldn't it have
                        > been better to have string literals be of type const char *?[/color]

                        Yes, except that by the time 'const' was added to the language
                        (circa 1989) there were 15 or so years of previous practice that
                        needed 'not breaking'.

                        If you are using gcc you can get the effect with -Wwrite-strings.

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

                          #13
                          Re: lower case to upper case

                          Keith Thompson wrote:[color=blue]
                          >
                          > CBFalconer <cbfalconer@yah oo.com> writes:[color=green]
                          > > Janice wrote:[color=darkred]
                          > >>
                          > >> char* line = "abcd";
                          > >> How to convert the line to upper case and print?
                          > >> Any option for printf to do this?[/color]
                          > >
                          > > You can't and no. However if you had defined line as:
                          > >
                          > > char line[] = "abcd";
                          > >
                          > > you would have been able to convert it. Now you should spend some
                          > > time thinking about what the difference is. It is fundamental.[/color]
                          >
                          > That depends on whether "convert" means to convert it in-place, or to
                          > create a converted copy. The problem statement doesn't make this
                          > clear.[/color]

                          It may mean to have the converted values
                          written to the standard output stream.

                          /* BEGIN new.c */

                          #include <stdio.h>
                          #include <ctype.h>

                          int main(void)
                          {
                          char *line = "abcd";

                          while(*line != '\0') {
                          putchar(toupper (*line));
                          ++line;
                          }
                          putchar('\n');
                          return 0;
                          }

                          /* END new.c */

                          --
                          pete

                          Comment

                          • Mark L Pappin

                            #14
                            the Rationale, string literals, and const (was: lower case to upper case)

                            Eric Sosman <eric.sosman@su n.com> writes:[color=blue]
                            > Mark A. Odell wrote:[/color]
                            [color=blue][color=green]
                            >> Wouldn't it have been better to have string literals be of type
                            >> const char *?[/color][/color]

                            (Or even "array of const char".)
                            [color=blue]
                            > According to the Rationale, string literals have this peculiar
                            > property to avoid breaking existing code[/color]

                            [snippage]

                            And this (almost useless, but certainly valid on a platform lacking
                            "const" ... like, say, that the Rationale uses to justify non-const
                            string literals) pre-C89 program is broken by that Standard:

                            main() {
                            char* const = "Hello, World!";
                            puts(const);
                            }

                            The "avoid breaking existing code" argument is trotted out all the
                            time in the Rationale, and it's bogus:

                            - If there is no existing standard, then defining anything that
                            conflicts with any existing usage anywhere will break existing code.
                            C89 broke existing code.

                            - If the new standard updates an old one, then defining anything other
                            than previously-undefined areas will break existing code.
                            C99 broke existing code.

                            C89 made sting literals non-const, and C99 didn't fix it, which
                            contributes to poor code now and in the future.

                            But don't blame "existing code" for it.
                            It's the fault of the Standard-setters alone.

                            <steps off soapbox>

                            mlp

                            Comment

                            • Lawrence Kirby

                              #15
                              Re: the Rationale, string literals, and const (was: lower case to upper case)

                              On Sat, 11 Dec 2004 15:50:54 +1000, Mark L Pappin wrote:
                              [color=blue]
                              > Eric Sosman <eric.sosman@su n.com> writes:[color=green]
                              >> Mark A. Odell wrote:[/color]
                              >[color=green][color=darkred]
                              >>> Wouldn't it have been better to have string literals be of type
                              >>> const char *?[/color][/color]
                              >
                              > (Or even "array of const char".)
                              >[color=green]
                              >> According to the Rationale, string literals have this peculiar
                              >> property to avoid breaking existing code[/color]
                              >
                              > [snippage]
                              >
                              > And this (almost useless, but certainly valid on a platform lacking
                              > "const" ... like, say, that the Rationale uses to justify non-const
                              > string literals) pre-C89 program is broken by that Standard:
                              >
                              > main() {
                              > char* const = "Hello, World!";
                              > puts(const);
                              > }
                              >
                              > The "avoid breaking existing code" argument is trotted out all the
                              > time in the Rationale, and it's bogus:[/color]

                              It may be bogus in some circumstances but it is certainly not inherently
                              bogus. Your example above is likely to affect a minority of programs, I
                              don't recall a massive outcry about this, which there would have been if
                              it broke a lot of code.

                              Also consider that it would be fairly trivial to fix this automatically
                              even for huge program sources: write a program that searches for the
                              word "const" in the context of an identifier and changes it to a valid
                              identifier that doesn't clash with others in the program.

                              The only difficulty would be if the identifier const was used in non-local
                              interface specifications e.g. in libraries.
                              [color=blue]
                              > - If there is no existing standard, then defining anything that
                              > conflicts with any existing usage anywhere will break existing code.
                              > C89 broke existing code.[/color]

                              Yes, but there's a matter of degree. Something that affects odd bits of
                              code here and there is not the same problem as something that affects
                              nearly the whole body of existing code. The fact is that code such as

                              char *str = "string";

                              was the normal, correct, mainstream way of doing things pre-C89.

                              Although there was no standards body ratified standard before C89, there
                              was still K&R 1 which provided an effective baseline for all C compiler
                              writers.
                              [color=blue]
                              > - If the new standard updates an old one, then defining anything other
                              > than previously-undefined areas will break existing code.
                              > C99 broke existing code.
                              >
                              > C89 made sting literals non-const, and C99 didn't fix it, which
                              > contributes to poor code now and in the future.
                              >
                              > But don't blame "existing code" for it.
                              > It's the fault of the Standard-setters alone.[/color]

                              There is no way that the standard committee could have made this change,
                              it breaks too much. C is if anything a pragmatic language, and one of its
                              big assets is the amount of C code that is already out there.

                              Lawrence

                              Comment

                              Working...