Thread: toupper() & tolower() problem?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,612
    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,612
    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
    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

  10. #10
    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;
    }

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> 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

  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 C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    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.
    They don't do the same thing anyway. The only situation that I can think of that they do same thing is when the value of the variable is not used. Like "++i;" or "i++;". In those cases the compiler will optimize, because it is too simple not to do so. You simply don't care about the "pre" vs "pro". You just have an increment.

    For custom type (classes) you can have something like this

    Code:
    myClass& operator++() //pre-increment
    {
        this->value += 1;
        return *this;
    }
    
    myClass operator++(int)  //post-increment
    {
        myClass temp(*this);
        this->value += 1;
        return tmp;
    }
    So the post increment will have to make a copy. The compiler will probably not optimize in this case.

    So post-increment is guaranteed to be equal or faster.

    If you know that they are the same (because they are fundamental types for example) then it is a matter of style. But that is an if. If you see a code that has
    Code:
    for (i=0; i < count; i++) ...
    then "i" might not be an int. It can be a custom class. In which case the "i++" would probably be wrong.
    if you see
    Code:
    for (int i=0; i < count; i++) ...
    then you know "i" is an int and no problem.

    If you don't overload ++ or -- I don't think you will have to worry which one to use...

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

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