toupper wont work

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

    toupper wont work

    Hi

    Im gettting run time error (Unhandled exeption, access violations) when
    executing the following code:

    #include <conio.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string>
    #include <stdlib.h>

    int main()
    {

    char *t="test";

    printf(t);

    *t = toupper(*t); //error here

    printf(t);

    getch();
    return 0;

    }
    I running it on VC++ 6

    All I want to do is to convert a string to upper case. Am I missing
    something here?

    Thanks


  • John Carson

    #2
    Re: toupper wont work

    "ben" <ben@ben.com> wrote in message
    news:d%AZb.3045 7$fW.22950@twis ter.rdc-kc.rr.com[color=blue]
    > Hi
    >
    > Im gettting run time error (Unhandled exeption, access violations)
    > when executing the following code:
    >
    > #include <conio.h>
    > #include <stdio.h>
    > #include <ctype.h>
    > #include <string>
    > #include <stdlib.h>
    >
    > int main()
    > {
    >
    > char *t="test";
    >
    > printf(t);
    >
    > *t = toupper(*t); //error here
    >
    > printf(t);
    >
    > getch();
    > return 0;
    >
    > }
    > I running it on VC++ 6
    >
    > All I want to do is to convert a string to upper case. Am I missing
    > something here?
    >
    > Thanks[/color]

    toupper doesn't convert strings, it converts characters. As it happens, this
    is not your problem. Your problem is that

    char *t="test";

    creates a pointer to a string literal that is stored in read-only memory.
    Thus you cannot change it. What you need is:

    char t[]="test";

    which creates a writeable array.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)


    Comment

    • ben

      #3
      Re: toupper wont work

      thanks, it worked

      "John Carson" <donaldquixote@ datafast.net.au > wrote in message
      news:4036e008@u senet.per.parad ox.net.au...[color=blue]
      > "ben" <ben@ben.com> wrote in message
      > news:d%AZb.3045 7$fW.22950@twis ter.rdc-kc.rr.com[color=green]
      > > Hi
      > >
      > > Im gettting run time error (Unhandled exeption, access violations)
      > > when executing the following code:
      > >
      > > #include <conio.h>
      > > #include <stdio.h>
      > > #include <ctype.h>
      > > #include <string>
      > > #include <stdlib.h>
      > >
      > > int main()
      > > {
      > >
      > > char *t="test";
      > >
      > > printf(t);
      > >
      > > *t = toupper(*t); //error here
      > >
      > > printf(t);
      > >
      > > getch();
      > > return 0;
      > >
      > > }
      > > I running it on VC++ 6
      > >
      > > All I want to do is to convert a string to upper case. Am I missing
      > > something here?
      > >
      > > Thanks[/color]
      >
      > toupper doesn't convert strings, it converts characters. As it happens,[/color]
      this[color=blue]
      > is not your problem. Your problem is that
      >
      > char *t="test";
      >
      > creates a pointer to a string literal that is stored in read-only memory.
      > Thus you cannot change it. What you need is:
      >
      > char t[]="test";
      >
      > which creates a writeable array.
      >
      >
      > --
      > John Carson
      > 1. To reply to email address, remove donald
      > 2. Don't reply to email address (post here instead)
      >
      >[/color]


      Comment

      • Frank Schmitt

        #4
        Re: toupper wont work

        "ben" <ben@ben.com> writes:
        [color=blue]
        > Hi
        >
        > Im gettting run time error (Unhandled exeption, access violations) when
        > executing the following code:
        >
        > #include <conio.h>
        > #include <stdio.h>
        > #include <ctype.h>
        > #include <string>
        > #include <stdlib.h>
        >
        > int main()
        > {
        >
        > char *t="test";
        >
        > printf(t);[/color]

        This is asking for trouble - use
        print("%s",t);[color=blue]
        >
        > *t = toupper(*t); //error here
        >
        > printf(t);
        >
        > getch();
        > return 0;
        >[/color]

        Better yet, get rid of C-style strings altogether and use C++ strings
        and I/O:

        #include <cctype>
        #include <string>
        #include <iostream>

        int main() {

        std::string t = "test";
        std::cout << t << "\n";
        for (std::size_t i=0; i<t.length(); ++i)
        t[i] = std::toupper(t[i]);
        std::cout << t << std::endl;
        return 0;
        }

        HTH & kind regards
        frank

        --
        Frank Schmitt
        quattro research GmbH
        e-mail: schmitt NO at SPAM quattro-research !@! dot com

        Comment

        • Dietmar Kuehl

          #5
          Re: toupper wont work

          Pete Becker <petebecker@acm .org> wrote:[color=blue]
          > Frank Schmitt wrote:[color=green][color=darkred]
          > > > char *t="test";
          > > > printf(t);[/color]
          > >
          > > This is asking for trouble - use
          > > print("%s",t);[/color]
          >
          > There's nothing wrong wtih printf(t).[/color]

          Well, in this case there is nothing wrong with 'printf(t)'. If 't' is obtained
          from somewhere, eg. from user input, it is not really a bright idea to use
          't' as format string, however: it may contain formatting directives by
          accident in which case the call to 'printf(t)' might easily yield funny
          behavior.

          Of course, this is not really related to the original problem which was
          writing to string literal. The other problem not yet noted in this thread
          was the fact that 'char' may be signed but 'toupper()' has to be called with
          an 'unsigned char' or EOF (this is described in 7.4 paragraph 1 of the current
          C standard; I don't have the C90 standard but I'm sure the same rule applies
          there, too). Thus, the program should look something like this:

          char t[] = "test";
          for (char* it = t; *it; ++it)
          *it = std::toupper(st atic_cast<unsig ned char>(*it));

          Of course, for "test" it is again unlikely to make any difference because
          normally these characters happen to be represented by ASCII which uses all
          positive values. However, in a real application the characters may include
          non-ASCII character which may have negative values. Of course, in this case
          there may be other problems as well: for example, the string may include the
          German "sz" ligature for which there is no single character uppercase variant.
          The uppercase variante of the "sz" ligature (which is just one character) has
          two characters.
          --
          <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
          Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>

          Comment

          Working...