Home > Net >  How can I simplify the For loop(c )?
How can I simplify the For loop(c )?

Time:11-01

I found a code on the internet to encrypt user input with Caesar encryption. But in the code the loop head bother me, because we didn't have things like "message[i]" or "\0" in class. Is it possible to write this in a different way? But we had not used arrays as far as in this loop header. This is not homework or anything like that. I'm practicing for my computer science test next week and there will probably be something similar. The loop header always looked like this for example for(i = 0; i < 4; i ). How can I write this code without arrays?

How can I write the loop differently? Or do I have to change other code parts?

#include <iostream>

using namespace std;

int main()
{
    char message[100], ch;
    int i, key;
    cout << "Enter a message to encrypt: ";
    cin.getline(message, 100);
    cout << "Enter key: ";
    cin >> key;
    for (i = 0; message[i] != '\0';   i)
    {                    //<-
        ch = message[i]; //<-
        if (ch >= 'a' && ch <= 'z')
        {
            ch = ch   key;
            if (ch > 'z')
            {
                ch = ch - 'z'   'a' - 1;
            }
            message[i] = ch; //<-
        }
        else if (ch >= 'A' && ch <= 'Z')
        {
            ch = ch   key;
            if (ch > 'Z')
            {
                ch = ch - 'Z'   'A' - 1;
            }
            message[i] = ch; //<-
        }
    }
    cout << "Encrypted message: " << message;
    return 0;
}

CodePudding user response:

To have a for loop closer to what you are used to, we need to know how many letters were input. The smallest change that does that is to use strlen to count them.

for (i = 0; i < strlen(message);   i)

However it's better to use std::string to hold text, because that knows it's size.

int main()
{
    std::string message;
    int key;
    std::cout << "Enter a message to encrypt: ";
    std::getline(std::cin, message);
    std::cout << "Enter key: ";
    std::cin >> key;
    for (i = 0; i < message.size();   i)
    {
        char ch = message[i];
        if (ch >= 'a' && ch <= 'z')
        {
            ch = ch   key;
            if (ch > 'z')
            {
                ch = ch - 'z'   'a' - 1;
            }
            message[i] = ch; //<-
        }
        else if (ch >= 'A' && ch <= 'Z')
        {
            ch = ch   key;
            if (ch > 'Z')
            {
                ch = ch - 'Z'   'A' - 1;
            }
            message[i] = ch; //<-
        }
    }
    std::cout << "Encrypted message: " << message;
    return 0;
}

And even better than that, you can loop over the chars in a string directly

int main()
{
    std::string message;
    int key;
    std::cout << "Enter a message to encrypt: ";
    std::getline(std::cin, message);
    std::cout << "Enter key: ";
    std::cin >> key;
    for (char & ch : message) // <- N.b. char &, we are modifying the `char` objects owned by message
    {
        if (ch >= 'a' && ch <= 'z')
        {
            ch = ch   key;
            if (ch > 'z')
            {
                ch = ch - 'z'   'a' - 1;
            }
        }
        else if (ch >= 'A' && ch <= 'Z')
        {
            ch = ch   key;
            if (ch > 'Z')
            {
                ch = ch - 'Z'   'A' - 1;
            }
        }
    }
    std::cout << "Encrypted message: " << message;
    return 0;
}

CodePudding user response:

#include <iostream>
#include <cctype>
  
using namespace std;
  
int main() {
    char message[100];
    int key;
    
    cout << "Enter a message to encrypt: ";
    cin.getline(message, 100);
    
    cout << "Enter key: ";
    cin >> key;
    
    for (int i = 0; message[i] != '\0'; i  ) {
        if (isalpha(message[i]) == 0)
            continue;

        char ch = message[i]   key;

        if (isalpha(ch) == 0) {
            if (ch > 'Z')
                ch = 'A'   ch % 'Z'
            else
                ch = 'a'   ch % 'z';
        }

        message[i] = ch;
    }

    cout << "Encrypted message: " << message;
    
    return 0;
}
  • Related