Thread: Islower, isupper

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    3

    Islower, isupper

    Hi, I'm busy with an exercise for a course. Ik have to make ap program that codes tekst by changing the letters from the alphabet. f.e. Key = 2, so a is c and l is n etcetera.

    Now I want the uppercase letters tot remain uppercase ande the lowercase letters lowercase. I thought I had a nice solution, but it doesn't work. What am I doing wrong?

    insert
    Code:
    #include <cs50.h>
    #include <stdio.h>
    int main(void)
    {
        int key;
        //get key//
        do {
        printf("Enter your key (0-26)\n");
        key=get_int();
        }
        while (key<0 || key>26);
        //get plaintext//
        char text[50];
        printf("Enter your massage (max 50 characters): ");
        fgets(text, sizeof(text), stdin);
        //encipher//
        int x;
        for(x=0;x<50;x++)
            {
                if (isupper(text[x]))
                {
                    printf("%c",(text[x]+key)%97);
                }
                else if (islower(text[x]))
                {
                    printf("%c",(text[x]+key)%65);
                }
                else
                {
                    break;
                }
            }
    return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's plug in (text[x]+key)%97 with text[x] being 'a' and key == 2: ('a' + 2) % 97 => (97 + 2) % 97 => 99 % 97 => 2.

    As you stated yourself, you want the result to be 'c', i.e., 99, but you received a result of 2. Clearly, it is your transforming expression that is wrong. You're probably trying to account for things like 'z' + 1, but didn't do so correctly.

    Also, you shouldn't just assume that x < 50 is the correct loop condition: the user can enter less than 50 characters, and in fact the maximum string length is 49 (due to the terminating null character).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am having problems with isdigit, isalpha, isupper.
    By Yoonjong Kook in forum C Programming
    Replies: 2
    Last Post: 10-28-2015, 01:54 PM
  2. isDigit() and isLower() for strings
    By John_L in forum C Programming
    Replies: 2
    Last Post: 12-06-2007, 09:52 AM
  3. Help w/isupper function
    By Nicholas35 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2002, 06:54 AM

Tags for this Thread