Newbie C++, need help in capitalisation

R

Robbo

Hi, I need to know how to turn a string into Upper case letters even
if the input is in lower.

E.g. say i was entering a name in a program and i typed john smith, i
need it to come out as John Smith.

Any idea on how this is done?

Many Thanks
 
T

Tim Love

Hi, I need to know how to turn a string into Upper case letters even
if the input is in lower.
E.g. say i was entering a name in a program and i typed john smith, i
need it to come out as John Smith.
Any idea on how this is done?
Try searching for
toupper C++
in Google, or reading previous items in this newsgroup.
 
S

Sumit Rajan

Robbo said:
Hi, I need to know how to turn a string into Upper case letters even
if the input is in lower.

E.g. say i was entering a name in a program and i typed john smith, i
need it to come out as John Smith.

Any idea on how this is done?

Many Thanks
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
std::string str("john smith");
std::transform(str.begin(),str.end(),str.begin(),std::toupper);
std::cout << str << '\n';
}
 
S

Sumit Rajan

Sumit said:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
std::string str("john smith");
std::transform(str.begin(),str.end(),str.begin(),std::toupper);
std::cout << str << '\n';
}

I just re-read your post. I had initially thought you wanted all caps in
the result. If you need to capitalize only the first letter of every
word, the answer still is std::toupper(). Only a few more lines of code. :-;
 
P

Pedro Graca

Robbo said:
E.g. say i was entering a name in a program and i typed john smith, i
need it to come out as John Smith.

How do you deal with "John von Neumann"? :)
 
P

Pedro Graca

I (Pedro Graca) wrote...
[snip]
How do you deal with "John von Neumann"? :)

With a great deal of humility and respect.
(Sorry, it had to be done.)
Socks

Sorry if I hurt your feelings.
I just googled for "von" and typed the first name on the excerpts.
I've heard the name before but, to me, it's just any name that could be
the input to the function the OP is designing (I could just as well have
written "Pedro von Graca").

I just wanted the OP to have present that not everybody likes every part
of his/her name capitalized (or removed).


Unlike yours, my mail address is valid. If you want to continue this
conversation, please use e-mail.
 
J

John Harrison

With a great deal of humility and respect.
(Sorry, it had to be done.)
Socks

After the second world war John von Neumann advocated a pre-emptive nuclear
strike on the Soviet Union before it had the chance to develop nuclear
weapons. He was also (reputedly) one of the inspirations for Dr Strangelove.
He was a genius but I'm not sure how much I respect him, I certainly
wouldn't want to show any humility.

John
 
P

Pedro Graca

Pedro said:
I (Pedro Graca) wrote...
[snip]
How do you deal with "John von Neumann"? :)

With a great deal of humility and respect.
(Sorry, it had to be done.)
Socks

Sorry if I hurt your feelings.
[shameful snip]

I have no idea what I was thinking.

Probably I read (about a dozen times) "What a great deal of
humility and respect." and not a direct answer to the question.


Please everybody accept my apologies, especially Socks.
 
S

Siemel Naran

Robbo said:
E.g. say i was entering a name in a program and i typed john smith, i
need it to come out as John Smith.

Given a pointer to the first letter in "john smith". Capitalize the letter,
j to J. Use !std::isspace(ch) to increment the pointer to the space char
between the two words. Increment the pointer one more time to point to the
start of "smith". Repeat.

void capfirst(char * s) {
for ( ; *s; ++s) {
*s = std::toupper(c);
for (++s; *s && !std::isspace(*s); ++s) ;
if (!*s) break;
}
}

But we have some special cases to deal with:

(1) Sentence starts with a space, like " john smith".
(2) Sentence has two spaces between words, like "john smith".
(3) Sentence ends with a space.

Also, more technical

(4) Need to document what happens when user calls capfirst(NULL). A program
crash is OK, but should be documented.
(5) Perhaps caching the result of *s into a variable like char c; would make
it faster, but don't worry about this for now.
 
J

Joe Laughlin

Sumit Rajan said:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
std::string str("john smith");
std::transform(str.begin(),str.end(),str.begin(),std::toupper);
std::cout << str << '\n';
}

So, what went wrong here? Do I have a bad compiler or something?

fatire:[** NONE **]:/home/mz652c% cat test.cpp
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
std::string str("john smith");
std::transform(str.begin(),str.end(),str.begin(),std::toupper);
std::cout << str << '\n';
}

fatire:[** NONE **]:/home/mz652c% g++ -o test test.cpp
test.cpp: In function `int main()':
test.cpp:9: no matching function for call to `transform(
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'

fatire:[** NONE **]:/home/mz652c% g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with:
.../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--enable-shared --enable-threads=posix --disable-checking --with-system-zli
b --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top