Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:53:53+00:00 2026-06-15T11:53:53+00:00

My goal is to make a Vigenere cipher. I am attempting to do this

  • 0

My goal is to make a Vigenere cipher. I am attempting to do this by getting the key from argv, getting the string from the user, then passing the message and key though a function I made which combined them and returns the new value before printing it. For some reason it is just printing the key. I think it has something to do with the new function and how I am trying to use the returned value. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int new_crypt;
int encrypt(string , int );
int main(int argc, string argv[])
{
    if( argc != 2)
    {
        printf("Plese enter only one key");
        return 1;
    }

    string message = GetString();
    for(int i=0; i < strlen(message); i++)
    {
        int klen = strlen(argv[1]);
        //repeats the key until the message is over
        int key= argv[1][i%klen];
        bool kupper = isupper(key);
        bool klower = islower(key);
        bool kalpha = isalpha(key);
        if(kupper == true){
            //ASCII value of A is 65. 'A' = 0 shifts
            int k = key-65;
            int new_crypt = encrypt(message, k);
            printf("%c", new_crypt);
        }
        if(klower == true){
            //ASCII value of 'a' is 97. 'a' = 0 shifts
            int k = key- 97;
            int new_crypt = encrypt(message, k);
            printf("%c", new_crypt);
        }
        if(kalpha == false){
            int k = 0;
            int i = i-1;
            int new_crypt = encrypt(message, k);
            printf("%c", new_crypt);
        }

    }

    printf("\n");
    return 0;
}
int encrypt(string message, int k)
{
    for(int i=0; i < strlen(message); i++)
    {
        bool upper = isupper(message[i]);
        if(upper == true)
        {   //Makes sure the message doesnt go past 'Z'.. If it does it mod 90 it                                            /                   // and adds 65 ('A')
            int crypt = (message[i]+ k) % 90;
            if(crypt < 65)
            {
                int new_crypt = (crypt + 65);
                return new_crypt;
            }
            else{
                int new_crypt = crypt;
                return new_crypt;
            }
        }
        bool lower = islower(message[i]);
        if(lower == true)
        {
            int crypt = (message[i]+ k) % 123;
            if(crypt < 97)
            {
                int new_crypt = crypt + 97;
                return new_crypt;
            }
            else{
                int new_crypt = crypt;
                return new_crypt;
            }
        }
        bool alpha = isalpha(message[i]);
        if(alpha == false)
        {
            int new_crypt = message[i];
            return new_crypt;
        }
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:53:55+00:00Added an answer on June 15, 2026 at 11:53 am

    The loop in the encrypt function is completely useless, because there is no path through the loop-body without a return statement causing the loop to be terminated and control returned to the caller of encrypt. This makes that the program as a whole repeatedly encrypts the first character of the message with successive elements from the key.

    The easiest way around this is to make the following changes

    • Remove the loop from the encrypt function
    • Pass, as an additional argument, the element from the message that you want to encrypt, making the signature

      int encrypt(string message, int k, int i)
      

    Some miscellaneous remarks:

    • The global variable new_crypt is not used anywhere. You can safely remove it. (You should avoid the use of global variables as much as reasonably possible).
    • Instead of using the magic number 65, you can also use the character literal 'A'. This has the advantage that you don’t need a comment to explain the number 65 and that it is always the correct value for the capital A, even if you end up not using ASCII. (But see also the next bullet)
    • Your code assumes the letters A to Z (and a to z) have contiguous values (such that Z == A+26). This may happen to be the case for the English alphabet in the ASCII encoding, but it is not guaranteed for other language alphabets or encodings.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with this class. the goal is to make the main
My goal is to make the Delete key (without any modifiers) move selected files
I'm basically trying to make a goal bar. The goal is determined by getting
Goal: Make a evaluation of the picture's format, width and height and then saving
My goal is to make screenshot from window without displaying it. Code: All the
The goal is to make redirect from /Swiss+Military/something.. to /Swiss+Military+Hanowa/something.. I've tried: RewriteCond %{REQUEST_URI}
My goal is to make a simple GUI which almost look like this attached
My goal is to make sure the user may enter a list of item
Goal is to make a dialog that appears on menu_key pressed, but it keeps
My goal is to make a layout that is 200% width and height, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.