Thread: toupper() & tolower() problem?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    toupper() & tolower() problem?

    The following code converts the string copied to uppercase and then to lowercase again.

    Code:
    // Convert a string to uppercase & lowercase
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
     char str[80];
     int i;
     
     strcpy(str, "This is copied to str");
     for(i=0;str[i];i++)
                        str[i]=toupper(str[i]);
     cout << str;
     
     for(i=0;str[i];i++)
                        str[i]=tolower(str[i]);
     
     cout << "\nOriginal str was ( " << str << " )\n\n";
     
    system("pause");
    return 0;
    
    }
    The problem is I cannot make it work without a second for loop. I don't want to use the second for loop, instead any other way if possible.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    I don't really sure about what am I thinking..
    why don't you use, strupper and strlower?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Well it would be very useful if you show me how to do it, as I am a beginner in C++. I have not used strupper and strlower before.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Well the problem lies with your second condition of for loop

    it should be i < strlen(str)

    your modified code will be like this

    Code:
     for(i=0; i < strlen(str); i++)

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you're willing to make more copies of the original string, you can just copy a lowercase version and an uppercase version, all using one loop.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by whiteflags View Post
    If you're willing to make more copies of the original string, you can just copy a lowercase version and an uppercase version, all using one loop.
    that is one point of optimization and others what i can suggest use ++i instead of i++ and user memset.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    None of those things are optimizations, and even if they were, strlen being called every iteration makes it irrelevant.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by whiteflags View Post
    None of those things are optimizations, and even if they were, strlen being called every iteration makes it irrelevant.
    I don't know how can u say that

    for post and pre increment

    "preincrement (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied". And it means pre increment is better than the post one

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Guys can't we use the transform function here like the one used in the following code?
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    #include <cstring>
    #include <cstdlib>
    
    int main()
    {
        char string[100];
        char upString[100];
        char lowString[100];
        
        strcpy(string, "I have a nice red house and you're not gonna take it from me!");
        
        unsigned int stringLength = 0;
        for (unsigned int i = 0; string[i] != '\0'; i++)
            stringLength++;
        
        std::cout << "The original: " << string << "\n\n";
        std::cout << "Now applying transform() to uppercase...\n\n";
        
        std::transform(&string[0], &string[stringLength], &upString[0], toupper);
    // This is equal to
    // std::transform(string, string + stringLength, upString, toupper);
    // but might make more sense
        upString[stringLength] = 0;
        
        std::cout << "upString: " << upString << "\n\n";
        std::cout << "Now applying transform() to lowercase (it's a verb, yes)...\n\n";
        
        std::transform(&string[0], &string[stringLength], &lowString[0], tolower);
        lowString[stringLength] = 0;
        
        std::cout << "lowString: " << lowString << "\n\n";
        
        system("pause");
        return 0;
    }

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> i dont know how you can say that

    What do you think takes more time, counting the number of characters in the string or copying a single integer? Now square that estimate, because you called it for every iteration. Thinking in theory is fine, but you should use common sense first.

    >> Guys can't we use the transform function here like the one used in the following code?

    What do you think

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by RockyMarrone View Post
    I don't know how can u say that

    for post and pre increment
    For simple types like ints, the compiler would optimize i++ to ++i for you anyways, but yes, I always use ++i in for loops anyways.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Yes, we should prefer ++i to i++.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, we should not prefer one over the other. It is a matter of style and where one form fits better than the other. Neither is better than the other.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    I see. Thank you.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It matters for non-pointer iterators, so it's a good habit to use ++i, so that ++itr is consistent.

    Re:the OP
    Just have a seperate buffer for the uppercase and lowercase strings.
    Last edited by King Mir; 11-13-2009 at 09:52 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. tolower function problem
    By Jack-Bauer in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 11:17 PM