ascii function?

B

Ben Bateman

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

Ben Bateman

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

Gianni Mariani

Ben said:
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?

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<char>(
toupper('a') ) << std::endl;

std::string s="hello";
transform(s.begin(), 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
 
J

John Harrison

Gianni Mariani said:
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<char>(
toupper('a') ) << std::endl;

std::string s="hello";
transform(s.begin(), 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

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
 
B

Ben Bateman

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;

}
 
G

Gianni Mariani

John said:
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.
 
R

Roel Schroeven

Alf said:
Since toupper has unsigned _int_ argument a negative value ends up as
a value outside the range of unsigned char.




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.

It seemed to work fine with a Belgian Dutch locale, though. Never had
any problems with it.
The int that toupper accepts is an unsigned int (see above).

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

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

Alf P. Steinbach

[The MS 'Class Wizard'] seemed to work fine with a Belgian Dutch
locale, though. Never had any problems with it.

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.


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:

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
 

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
474,113
Messages
2,570,690
Members
47,269
Latest member
VitoYwo03

Latest Threads

Top