Thread: function to make upper case alphabet not working

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    7

    function to make upper case alphabet not working

    Still trying to play around with pointers, thought I would make this function. For some reason, it's not working and it's giving me this error.
    I made it in a seperate file though.
    Code:
    int strlen(const char* string){
        int n = 0;
        for(;;)
        {
            if(string[n] == '\0') break;
            n++;
        }
        return n;
    }
    void to_upper(char *string)
    {
        for(int i = 0, n = strlen(string); i < n; i++)
        {
            if( string[i] > 122 || string[i] < 97 ) continue;
            (int) string[i] += 32;
        }
    }
    now I'll call it in main
    Code:
    int main(){
        char *name = "Aahmed";
        to_upper(name);
        printf("%s\n", name);
        return 0;
    }
    giving me this error on compiling
    error: lvalue required as left operand of assignment

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should not name your function strlen as that is a name from the standard library. You can use a name prefix to differentiate it from the standard library function.

    The error is because you're casting string[i], and you cannot assign to the result of a cast. Get rid of that cast.

    You should also avoid magic numbers (in this case by using character constants), and where you have to have a magic number, document what it means (either by naming it with a macro, or by a comment).

    In main, you have another problem: name is a pointer to a string literal, therefore it cannot be modified. Change name into an array.

    EDIT:
    Also, it looks like you have a logic error: you probably want to subtract 32 rather than add.
    Last edited by laserlight; 05-05-2020 at 05:30 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    You should not name your function strlen as that is a name from the standard library. You can use a name prefix to differentiate it from the standard library function.

    The error is because you're casting string[i], and you cannot assign to the result of a cast. Get rid of that cast.

    You should also avoid magic numbers (in this case by using character constants), and where you have to have a magic number, document what it means (either by naming it with a macro, or by a comment).

    In main, you have another problem: name is a pointer to a string literal, therefore it cannot be modified. Change name into an array.
    I have done what you said in green and totally understood it.

    What you have said in blue, is it really impossible for me to change a string literal in C?

    I didn't understand what's in red, what do you mean by magic numbers? It's the ASCII code for the characters.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 7usseiny
    What you have said in blue, is it really impossible for me to change a string literal in C?
    Yes and no. Yes, in that it results in undefined behaviour, so it might or might not work, and could well result in a crash when it doesn't work. No, because it might work... only for your program to crash when you demonstrate your program to your teacher who will then award you a zero, or to the big boss who will then fire you.

    Quote Originally Posted by 7usseiny
    I didn't understand what's in red, what do you mean by magic numbers? It's the ASCII code for the characters.
    Quick, without referring to the ASCII table, what is the character corresponding to the ASCII code 35? If you took more than 1 ms to come up with the correct answer, you're too slow, and so this is a "magic number": readers are unlikely to immediately know what 35 means, even if they might know that it is the value of a character in ASCII. Therefore, you should prefer to use '#' instead of 35. Similiarly, what's 32, and are you sure it is correct? You could #define LOWERCASE_UPPERCASE_DIFF ('a' - 'A') and then use LOWERCASE_UPPERCASE_DIFF instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Yes and no. Yes, in that it results in undefined behaviour, so it might or might not work, and could well result in a crash when it doesn't work. No, because it might work... only for your program to crash when you demonstrate your program to your teacher who will then award you a zero, or to the big boss who will then fire you.


    Quick, without referring to the ASCII table, what is the character corresponding to the ASCII code 35? If you took more than 1 ms to come up with the correct answer, you're too slow, and so this is a "magic number": readers are unlikely to immediately know what 35 means, even if they might know that it is the value of a character in ASCII. Therefore, you should prefer to use '#' instead of 35. Similiarly, what's 32, and are you sure it is correct? You could #define LOWERCASE_UPPERCASE_DIFF ('a' - 'A') and then use LOWERCASE_UPPERCASE_DIFF instead.
    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upper case= lower case
    By ashley1nonly in forum Tech Board
    Replies: 3
    Last Post: 02-19-2015, 12:52 AM
  2. Upper case= lower case
    By ashley1nonly in forum C Programming
    Replies: 0
    Last Post: 02-18-2015, 10:24 PM
  3. Replies: 4
    Last Post: 12-06-2014, 01:51 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM

Tags for this Thread