473,474 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ - To UpperCase?

I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?

Here is what I got.

cout << "Enter title of a book for look up: ";
cin >> firstString;

if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)

then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?
Any help would be great! Thanks!

Oct 18 '05 #1
13 29240
GRoll21 wrote:
I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?
'toupper' should do it. But you don't need to uppercase a char, do you?
You need to uppercase a whole array of them, probably...
Here is what I got.

cout << "Enter title of a book for look up: ";
cin >> firstString;

if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)
What's a 'firstString'?
then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?


So, it's an array, eh? Then you need to iterate through the array and
apply 'toupper' to every element. It's that simple.

V
Oct 18 '05 #2
firstString is the book the user inputs.

cout << "Enter title of a book for look up: ";
cin >> firstString;

Oct 18 '05 #3
GRoll21 wrote:
I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?

Here is what I got.

cout << "Enter title of a book for look up: ";
cin >> firstString;

if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)

then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?
Any help would be great! Thanks!


If you want to make the string all uppercase then just go through the
string and convert all the characters:

const char* strtoupper(string str)
{
for (int i=0;i<str.size();i++)
str[i] = toupper(str[i]);
return str.c_str();
}

Oct 18 '05 #4
On Tue, 18 Oct 2005 15:48:13 -0700, GRoll21 wrote:
I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?

Here is what I got.

cout << "Enter title of a book for look up: ";
cin >> firstString;

if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)

then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?


If you just want to compare, you can use the case-insensitive version of
strcmp. Now is it strcmpi or stricmp? (I'm not sure. :)

- Jay
Oct 19 '05 #5
I wrote a set o f C++ classes that handle different types of string
manipulations one of string manipulations that is most needed by
programmers is the ability to convert the string to upper case:

const std::string& jme::strtools::toUpper( const std::string& s ) {
tmp = s;
tmp = this->trimIt( tmp );
for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
{
*i = std::toupper( *i );
}
return tmp;
}
we are developing this library to be released under the GNU linces. So,
if you are interested you can via AIM=jalqadir
for a free copy of this library.

Have fun!

Oct 19 '05 #6
jalkadir wrote:
const std::string& jme::strtools::toUpper( const std::string& s ) {
If you are working on a temporary copy anyway, why not simply declare
it like this:

const std::string& jme::strtools::toUpper( std::string s )

Saves a few lines of code and does the same...
tmp = s;
Where is tmp declared?
tmp = this->trimIt( tmp );
This function is called "toUpper()", why does it trim the string as
well?
for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
{
*i = std::toupper( *i );
}
return tmp;
}
How about:

std::transform( tmp.begin(), tmp.end(), tmp.begin(),
(int(*)(int))std::toupper );
we are developing this library to be released under the GNU linces. So,
if you are interested you can via AIM=jalqadir
for a free copy of this library.


Why not check it into sf.net or similar?
I'd like to see this library and monitor it's progress - maybe even
contribute. It would be easier that way.

Cheers,
Andre

Oct 19 '05 #7
On Tue, 18 Oct 2005 23:57:54 GMT, Jay Nabonne
<ja*********@sonicNOSPAM.com> wrote in comp.lang.c++:
On Tue, 18 Oct 2005 15:48:13 -0700, GRoll21 wrote:
I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?

Here is what I got.

cout << "Enter title of a book for look up: ";
cin >> firstString;

if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)

then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?


If you just want to compare, you can use the case-insensitive version of
strcmp. Now is it strcmpi or stricmp? (I'm not sure. :)

- Jay


There is no case-insensitive version of strcmp() in the standard C or
standard C++ library. What you are not sure of is the name of some
particular non-standard function on some specific implementation, or
perhaps the different names of several non-standard functions on
different implementations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 19 '05 #8
GRoll21 wrote:
firstString is the book the user inputs.

cout << "Enter title of a book for look up: ";
cin >> firstString;


That is obvious, what Victor meant (I assume) is have you declared
firstString like this

std::string firstString;

or something like this

char firstString[99];

These details matter.

But in either case the answer is similar. You have a function toupper
which converts a single character to uppercase, you have a string or a
char array which is basically a whole bunch of characters, so you have
to write a loop which uses toupper on each character in your string or
char array.

This is called programming, there always comes a point where the
standard functions run out and you have to write your own. You've just
reached it.

john
Oct 19 '05 #9
On 2005-10-19, jalkadir <ja******@gosonic.ca> wrote:
I wrote a set o f C++ classes that handle different types of
string manipulations one of string manipulations that is most
needed by programmers is the ability to convert the string to
upper case:

const std::string& jme::strtools::toUpper( const std::string& s ) {
tmp = s;
tmp = this->trimIt( tmp );
for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
{
*i = std::toupper( *i );
}
return tmp;
}


You should not return a reference or pointer to automatic storage
class variables. tmp is destroyed when the function returns,
leaving the caller with a useless reference to a nonexistent
string.

--
Neil Cerutti
Oct 19 '05 #10

Neil Cerutti wrote:
const std::string& jme::strtools::toUpper( const std::string& s ) {
tmp = s;
tmp = this->trimIt( tmp );
for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
{
*i = std::toupper( *i );
}
return tmp;
}


You should not return a reference or pointer to automatic storage
class variables. tmp is destroyed when the function returns,
leaving the caller with a useless reference to a nonexistent
string.

I suspect 'tmp' is a member of the 'strtools' class (or maybe a global
variable). In any case, since it is not declared in function scope, it
will *not* be destroyed when the function returns, so the reference is
still valid.

As to why we need this 'tmp' global variable is a good question. It
definitely kills any possibillity of doing multi-threaded programming.
I suspect the original author mistakenly thought it might be
significantly more efficient to use an existing variable.

-shez-

Oct 19 '05 #11
On 2005-10-19, Shezan Baig <sh************@gmail.com> wrote:

Neil Cerutti wrote:
> const std::string& jme::strtools::toUpper( const std::string& s ) {
> tmp = s;
> tmp = this->trimIt( tmp );
> for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
> {
> *i = std::toupper( *i );
> }
> return tmp;
> }


You should not return a reference or pointer to automatic storage
class variables. tmp is destroyed when the function returns,
leaving the caller with a useless reference to a nonexistent
string.

I suspect 'tmp' is a member of the 'strtools' class (or maybe a
global variable). In any case, since it is not declared in
function scope, it will *not* be destroyed when the function
returns, so the reference is still valid.


Ah. Whoops I didn't notice it went undeclared. Thanks for the
correction.

--
Neil Cerutti
Oct 19 '05 #12
GRoll21 wrote:
I know there is a function but I cannot seem to find it. There should
be a way to uppercase a char right?

then i check the book they enter to a book in my bookTitle array. How
can I make it so it puts both firstString and bookTitle to uppercase?
Any help would be great! Thanks!


There is the CRT function/macro toupper from <cctype>, which converts a
single character to upper case, using the C locale.
There is the ctype facet of the C++ locale from <locale> which has a member
toupper, which does the same, but using the C++ locale.
There is a global toupper function in <locale>, which is essentially a
shortcut to the ctype facet.

To work on entire strings at the same time, I recommend the Boost String
Algorithm library, available in Boost 1.32.0 and later.
http://www.boost.org/doc/html/string...html#id1290831

--
Sebastian Redl
Oct 19 '05 #13
On Tue, 18 Oct 2005 21:57:02 -0500, Jack Klein wrote:

There is no case-insensitive version of strcmp() in the standard C or
standard C++ library. What you are not sure of is the name of some
particular non-standard function on some specific implementation, or
perhaps the different names of several non-standard functions on
different implementations.


Well, that explains it, then. :)
Oct 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hallvard B Furuseth | last post by:
Has someone got a Python routine or module which converts Unicode strings to lowercase (or uppercase)? What I actually need to do is to compare a number of strings in a case-insensitive manner,...
5
by: Paul Schwann | last post by:
Hi everybody, I have a problem with the following code: #include <iostream> int main(void) { std::cout << std::hex << std::uppercase << 31 <<std::endl; return 0;
8
by: Markus Dehmann | last post by:
My ios::uppercase (and others) seems not to work. #include <iostream> using namespace std; int main(){ int num = 45; cout.setf(ios::hex | ios::showbase | ios::uppercase); cout << "number " <<...
2
by: Thomas McK | last post by:
Hi all, I'm trying to make a SQL query (against a table in MS Access) where I want to take the value of something, and check to see if the first two letters in that value are capitalized (for...
1
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name...
1
by: Beffmans | last post by:
How can I turn the Uppercase mode on in my TextBoxes? ch B. *** Sent via Developersdex http://www.developersdex.com ***
6
by: feeman | last post by:
I can change a field to upper case by using the after event function and the following code Me. = UCase(Me.) But how can you do it so that the whole form will change to Uppercase, there are...
6
by: 182719 | last post by:
<?php $testcase = 'AKLWC139'; if (ctype_upper($testcase)) { echo "The string $testcase consists of all uppercase letters. \n"; } else { echo "The string $testcase does not consist of all...
4
by: Hrsoft | last post by:
I'm a newbie with asp.net I need to transform a user textbox to uppercase, when typing. Is this possible? I read about Ajax Maskedit, but it is only to certain formats, like dates, etc Thanks...
0
jinu1996
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...
1
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.