ascii function?

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

    ascii function?

    I am working on a small program that needs to evaluate a string input by a
    user that will check for all the upper or lower case letters, compare them,
    give an option to covert the upper to lower and lower to upper and then
    display the changes on the screen.

    My instructor says that there is an ascii function that will help me do
    this, but I cannot find anything on the web or in the book that was given to
    me in the class to help me with this. I am completely stuck.



  • Ben Bateman

    #2
    Re: ascii function?

    I have those in my book but I don't know how or where to apply them into the
    compiler. Are these the only functions that i need to search through the
    string and compare the lcase and ucase letters?


    "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
    news:bemugu$cpv @dispatch.conce ntric.net...[color=blue]
    > Ben Bateman wrote:[color=green]
    > > I am working on a small program that needs to evaluate a string input by[/color][/color]
    a[color=blue][color=green]
    > > user that will check for all the upper or lower case letters, compare[/color][/color]
    them,[color=blue][color=green]
    > > give an option to covert the upper to lower and lower to upper and then
    > > display the changes on the screen.
    > >
    > > My instructor says that there is an ascii function that will help me do
    > > this, but I cannot find anything on the web or in the book that was[/color][/color]
    given to[color=blue][color=green]
    > > me in the class to help me with this. I am completely stuck.
    > >
    > >
    > >[/color]
    >
    > toupper
    > tolower
    > isupper
    > islower
    >
    >[/color]


    Comment

    • Gianni Mariani

      #3
      Re: ascii function?

      Ben Bateman wrote:[color=blue]
      > I have those in my book but I don't know how or where to apply them into the
      > compiler. Are these the only functions that i need to search through the
      > string and compare the lcase and ucase letters?
      >[/color]

      Here is an example of how to use these.



      #include <cctype>
      #include <iostream>
      #include <algorithm>

      int main()
      {

      std::cout << ( isupper( 'A' ) ? "A is upper case" : "A is not
      upper case" ) << std::endl;
      std::cout << ( isupper( 'a' ) ? "a is upper case" : "a is not
      upper case" ) << std::endl;

      std::cout << "toupper('a ') is " << static_cast<cha r>(
      toupper('a') ) << std::endl;

      std::string s="hello";
      transform(s.beg in(), s.end(), s.begin(), toupper);

      std::cout << "hello transformed by toupper is : " << s <<
      std::endl;
      }

      output:

      A is upper case
      a is not upper case
      toupper('a') is A
      hello transformed by toupper is : HELLO

      Comment

      • John Harrison

        #4
        Re: ascii function?


        "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
        news:ben056$cpv @dispatch.conce ntric.net...[color=blue]
        > Ben Bateman wrote:[color=green]
        > > I have those in my book but I don't know how or where to apply them into[/color][/color]
        the[color=blue][color=green]
        > > compiler. Are these the only functions that i need to search through[/color][/color]
        the[color=blue][color=green]
        > > string and compare the lcase and ucase letters?
        > >[/color]
        >
        > Here is an example of how to use these.
        >
        >
        >
        > #include <cctype>
        > #include <iostream>
        > #include <algorithm>
        >
        > int main()
        > {
        >
        > std::cout << ( isupper( 'A' ) ? "A is upper case" : "A is not
        > upper case" ) << std::endl;
        > std::cout << ( isupper( 'a' ) ? "a is upper case" : "a is not
        > upper case" ) << std::endl;
        >
        > std::cout << "toupper('a ') is " << static_cast<cha r>(
        > toupper('a') ) << std::endl;
        >
        > std::string s="hello";
        > transform(s.beg in(), s.end(), s.begin(), toupper);
        >
        > std::cout << "hello transformed by toupper is : " << s <<
        > std::endl;
        > }
        >
        > output:
        >
        > A is upper case
        > a is not upper case
        > toupper('a') is A
        > hello transformed by toupper is : HELLO
        >[/color]

        Unfortunely using toupper in that manner is not guaranteed to work according
        to the standard. toupper (and other similar functions) take an int as a
        parameter and will correctly deal with EOF or any *unsigned* char value.
        transform in the way you used it will not cast the char values within a
        string to unsigned char.

        john


        Comment

        • Ben Bateman

          #5
          Re: ascii function?

          Well honestly for ability level we just started programming 2 weeks ago, we
          have gone through for while and if loops, i understand how to use them, but
          mostly we just used integers

          Here is an example of what we started with:
          The problem is write a loop that will count 20 times and add a variable to
          itself 10 times. First we used a sequential loop, then we had to do it with
          a while loop.

          #include <iostream>
          using namespace std;

          main ()
          {
          int b=0;
          for (b=0;b<10;b++){
          cout<<b<<endl;

          }



          Here is the actual problem that was given:

          Read in a string
          Check to see if there are any capital letters
          If there are report the number of captial vs. lower
          then give the user the option of changing them all to upper or lower
          after the choice display the string according to their choice.

          This is all i can figure out so far, and my instructor told me that it was
          correct:

          #include <iostream>
          #include <string>
          #include <algorithm>
          #include <ctype.h>
          using namespace std;

          void main()
          {
          string x = "";
          cout<<"Type your sentence here: ";
          cin>>x;

          }



          Comment

          • Gianni Mariani

            #6
            Re: ascii function?

            John Harrison wrote:
            [color=blue]
            > Unfortunely using toupper in that manner is not guaranteed to work according
            > to the standard. toupper (and other similar functions) take an int as a
            > parameter and will correctly deal with EOF or any *unsigned* char value.
            > transform in the way you used it will not cast the char values within a
            > string to unsigned char.[/color]

            Please give an example of what you would do differently.


            Comment

            • Roel Schroeven

              #7
              Re: ascii function?

              Alf P. Steinbach wrote:[color=blue]
              > On Sat, 12 Jul 2003 10:06:17 GMT, Roel Schroeven <j4nff5m02@snea kemail.com> wrote:
              >[color=green]
              >>John Harrison wrote:[color=darkred]
              >>>
              >>> "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
              >>> news:benee8$cpv @dispatch.conce ntric.net...
              >>>> John Harrison wrote:
              >>>>
              >>>> > Unfortunely using toupper in that manner is not guaranteed to work
              >>> according
              >>>> > to the standard. toupper (and other similar functions) take an int as a
              >>>> > parameter and will correctly deal with EOF or any *unsigned* char value.
              >>>> > transform in the way you used it will not cast the char values within a
              >>>> > string to unsigned char.
              >>>>
              >>>> Please give an example of what you would do differently.
              >>>>
              >>>
              >>> Well this
              >>>
              >>> char safe_toupper(ch ar ch)
              >>> {
              >>> return toupper(static_ cast<unsigned char>(ch));
              >>> }
              >>>
              >>> transform(s.beg in(), s.end(), s.begin(), safe_toupper);
              >>>
              >>> If you are implying that you think that is unecessary then tell me why.[/color]
              >>
              >>If understand you correctly, you are saying that toupper() doesn't
              >>correctly handle the negative values of a signed char?[/color]
              >
              > Since toupper has unsigned _int_ argument a negative value ends up as
              > a value outside the range of unsigned char.
              >
              >
              >[color=green]
              >>If that is a problem, I don't think it is a problem. AFAIK the negative
              >>values of a char are not used in a string context, except possibly EOF.[/color]
              >
              > E.g., Norwegian 'æ', 'ø' and 'å'.
              >
              > But you're not alone.
              >
              > Microsoft also often thinks that world only consists of english-
              > speaking countries, with e.g. the result that their 'Class Wizard'
              > (in the days of MFC) didn't work unless you changed the PC locale to
              > US. And they never fixed that infernal bug, from Visual C++ 1.x
              > through 6.0. I can tell you that that angered a lot of non-US folks.[/color]

              It seemed to work fine with a Belgian Dutch locale, though. Never had
              any problems with it.
              [color=blue][color=green]
              >>And if it really is a problem, I don't see how safe_toupper solves it.
              >>By casting char to unsigned char, you gain nothing but can possibly lose
              >>something, while the int that toupper accepts can contain any value of
              >>the char.[/color]
              >
              > The int that toupper accepts is an unsigned int (see above).[/color]

              It's a signed int:

              int toupper (int c);

              Otherwise it wouldn't be able to handle EOF. But you're right that it
              doesn't handle negative char values:

              "If c is not an unsigned char value, or EOF, the behaviour of these
              functions is undefined."
              [color=blue][color=green]
              >>(Please correct me if I'm talking nonsens)[/color][/color]

              I apologize. Even though I live in a non-English-speaking country
              myself, I've never known very well how international character sets are
              handled in different computing environments. While I do know that the
              standard doesn't specify whether a char is signed or unsigned, I kinda
              assumed most environments use unsigned char to accomodate for char
              values > 127. Obviously I was wrong.

              --
              "Codito ergo sum"
              Roel Schroeven

              Comment

              • Alf P. Steinbach

                #8
                Re: ascii function?

                On Sat, 12 Jul 2003 11:11:50 GMT, Roel Schroeven <j4nff5m02@snea kemail.com> wrote:
                [color=blue]
                >
                >[The MS 'Class Wizard'] seemed to work fine with a Belgian Dutch
                >locale, though. Never had any problems with it.[/color]

                Curious. E.g., if you clicked a control to add an event handler that
                didn't work with the PC in Norwegian locale. But oh well, off-topic.


                [color=blue][color=green][color=darkred]
                >>>And if it really is a problem, I don't see how safe_toupper solves it.
                >>>By casting char to unsigned char, you gain nothing but can possibly lose
                >>>something, while the int that toupper accepts can contain any value of
                >>>the char.[/color]
                >>
                >> The int that toupper accepts is an unsigned int (see above).[/color]
                >
                >It's a signed int:
                >
                >int toupper (int c);
                >
                >Otherwise it wouldn't be able to handle EOF. But you're right that it
                >doesn't handle negative char values:[/color]

                I apologize.

                Somewhere in my head a group of neurons had erronously fabricated a
                reason for the function's otherwise inexplicable limitations.

                These neurons have all been fired.


                Cheers,

                - lfA

                Comment

                Working...