Newbie C++, need help in capitalisation

Discussion in 'C++' started by Robbo, Oct 12, 2004.

  1. Robbo

    Robbo Guest

    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
    Robbo, Oct 12, 2004
    #1
    1. Advertising

  2. Robbo

    Tim Love Guest

    (Robbo) writes:

    >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.
    Tim Love, Oct 12, 2004
    #2
    1. Advertising

  3. Robbo

    Sumit Rajan Guest

    Robbo wrote:
    > 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';
    }


    --
    Sumit Rajan <sumit DOT rajan AT gmail DOT com>
    Sumit Rajan, Oct 12, 2004
    #3
  4. Robbo

    Sumit Rajan Guest

    Sumit Rajan wrote:
    > Robbo wrote:
    >
    >> 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';
    > }
    >
    >


    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. :-;


    --
    Sumit Rajan <sumit DOT rajan AT gmail DOT com>
    Sumit Rajan, Oct 12, 2004
    #4
  5. Robbo

    Pedro Graca Guest

    Pedro Graca, Oct 12, 2004
    #5
  6. Robbo

    Guest

    Pedro Graca <> wrote in message news:<-berlin.de>...
    [snip]
    > How do you deal with "John von Neumann"? :)


    With a great deal of humility and respect.
    (Sorry, it had to be done.)
    Socks
    , Oct 12, 2004
    #6
  7. Robbo

    red floyd Guest

    wrote:
    > Pedro Graca <> wrote in message news:<-berlin.de>...
    > [snip]
    >
    >>How do you deal with "John von Neumann"? :)

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


    Or how about by only using Harvard architecture machines?
    red floyd, Oct 12, 2004
    #7
  8. Robbo

    Pedro Graca Guest

    wrote:
    > 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.

    --
    USENET would be a better place if everybody read:
    http://www.expita.com/nomime.html
    http://www.netmeister.org/news/learn2quote2.html
    http://www.catb.org/~esr/faqs/smart-questions.html
    Pedro Graca, Oct 12, 2004
    #8
  9. <> wrote in message
    news:...
    > Pedro Graca <> wrote in message
    > news:<-berlin.de>...
    > [snip]
    >> How do you deal with "John von Neumann"? :)

    >
    > 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
    John Harrison, Oct 13, 2004
    #9
  10. Robbo

    Pedro Graca Guest

    Pedro Graca wrote:
    > wrote:
    >> 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.

    --
    USENET would be a better place if everybody read:
    http://www.expita.com/nomime.html
    http://www.netmeister.org/news/learn2quote2.html
    http://www.catb.org/~esr/faqs/smart-questions.html
    Pedro Graca, Oct 13, 2004
    #10
  11. Robbo

    Siemel Naran Guest

    "Robbo" <> wrote in message

    > 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.
    Siemel Naran, Oct 14, 2004
    #11
  12. Robbo

    Joe Laughlin Guest

    "Sumit Rajan" <> wrote in message
    news:...
    > Robbo wrote:
    > > 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';
    > }
    >
    >


    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)
    Joe Laughlin, Oct 15, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jbl
    Replies:
    2
    Views:
    472
    Joe Smith
    Dec 16, 2004
  2. Patrick Hill

    asp login, I need help please!! Newbie

    Patrick Hill, Feb 7, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    692
    Rick Strahl [MVP]
    Feb 8, 2004
  3. Robbo
    Replies:
    3
    Views:
    433
    Howard
    Oct 12, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    559
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    490
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page