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. 7 7616
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" <gi*******@mariani.ws> wrote in message
news:be********@dispatch.concentric.net... Ben Bateman wrote: 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.
toupper tolower isupper islower
Ben Bateman wrote: 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
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:be********@dispatch.concentric.net... Ben Bateman wrote: 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
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
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;
}
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.
Alf P. Steinbach wrote: On Sat, 12 Jul 2003 10:06:17 GMT, Roel Schroeven <j4*******@sneakemail.com> wrote:
John Harrison wrote: "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:be********@dispatch.concentric.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(char ch) { return toupper(static_cast<unsigned char>(ch)); }
transform(s.begin(), s.end(), s.begin(), safe_toupper);
If you are implying that you think that is unecessary then tell me why.
If understand you correctly, you are saying that toupper() doesn't correctly handle the negative values of a signed char?
Since toupper has unsigned _int_ argument a negative value ends up as a value outside the range of unsigned char. 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.
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. 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.
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." (Please correct me if I'm talking nonsens)
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
On Sat, 12 Jul 2003 11:11:50 GMT, Roel Schroeven <j4*******@sneakemail.com> wrote: [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. 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.
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:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: chunhui_true |
last post by:
i have a class, it can read one line(\r\n ended) from string,when i
read line from utf8 string i can't get any thing!
maybe i should conversion utf8 to ascii??there is any function can
conversion...
|
by: Guoqi Zheng |
last post by:
How can I know the charset of string? I only need to know a string contains
only ascii characters or it contains more than just ascii, maybe some
chinese characters. how can I do this?
--
Kind...
|
by: Ger |
last post by:
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?
I found...
|
by: Jeff |
last post by:
Hi -
I'm setting up a streamreader in a VB.NET app to read a text file and
display its contents in a multiline textbox.
If I set it up with System.Text.Encoding.Unicode, it reads a unicode...
|
by: Kermit Piper |
last post by:
Hello,
I have a function that lets me convert one character and throw an alert
with the corresponding ASCII value, but what I am having trouble with
is applying it to a text box. What I'm trying...
| |
by: ChaosKCW |
last post by:
Hi
I am reading from an oracle database using cx_Oracle. I am writing to a
SQLite database using apsw.
The oracle database is returning utf-8 characters for euopean item
names, ie special...
|
by: kuukelekuu |
last post by:
I need to convert ascii chars to hex chars.
I searched the internet, found hex to ascii, but nowhere is there a
ascii to hex method created by anyone.
Can anyone help me with that?
Chears
|
by: Gregor Kovač |
last post by:
Hi!
Does DB2 handle extended ASCII table?
Example:
VALUES(CHR(65)) =A
VALUES(CHR(129)) =null, but according to www.asciitable.com should be u
with umlaut.
Any idea ?
|
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= |
last post by:
PEP 1 specifies that PEP authors need to collect feedback from the
community. As the author of PEP 3131, I'd like to encourage comments
to the PEP included below, either here (comp.lang.python), or...
|
by: silverburgh.meryl |
last post by:
Hi,
Is there a string function to trim all non-ascii characters out of a
string?
Let say I have a string in python (which is utf8 encoded), is there a
python function which I can convert that...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |