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 8702795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:44:32+00:00 2026-06-13T02:44:32+00:00

This might seem like a very simple solution to some people but I have

  • 0

This might seem like a very simple solution to some people but I have been trying to figure it out for a while now and its bugging me.

int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
 char c = 'A' + sum%26;

I am having trouble coming up with the solution math has never been a strong point of mine. the value of char c should equal 79 at the end of it all if p[i]=72 or 'H' and s2[i]=5.

This operation works wonderfully but I am trying to understand what is going on so I have tried to do it with a normal calculator and keep coming up with the wrong answer. What exactly is the correct order of operations and what should the values be?

Here is the complete source code that I have so far that works:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char s2[25];
    strcpy(s2, argv[1]);
    int keyLen = strlen(s2);
    printf("Please enter a string of text to be encrypted!\n");
    string p = GetString();
    for (int i = 0, n = strlen(p); i < n; i++)
    {
        if (isupper(p[i])){
        int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
        char c = 'A' + sum%26;
        printf("%c", c);
        }
    }
    printf("\n");
    printf("%d\n", keyLen);

}

The way I am doing this operation is:

int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
//int sum = (72-65) + (72 % 5) - 65) sum= -56
char c = 'A' + sum%26;
//char c = 65 + -56 %26 char c = -8

I obviously know my math is off but I thought I was doing it right when I run the operation through the debugger sum = 14 and char c = 79 or 'O' like it should.

  • 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-13T02:44:34+00:00Added an answer on June 13, 2026 at 2:44 am
    int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
    //int sum = (72-65) + (72 % 5) - 65) sum= -56
    

    Your misunderstanding is here.

    What the code is doing

    (s2[i % keyLen] - 'A')
    

    means

    (char_from_s2 - 'A')
    

    where char_from_s2 is the character in s2 at index i % keyLen, i.e. the character at position i wrapped around to fit in the length of the string.

    What your calculation shows

    Your calculation is doing something different:

    (char_from_p % keyLen) - 'A'
    

    You’re taking the character from p instead of from s2, and applying the % operator to the character value instead of the index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might seem like a very easy question for some of you folks, but
This might seem like a very simple question, but I am struggling with it.
This might seem ridiculously simple, but I've been getting all kinds of error depending
I am just looking for some guidelines, as this might seem like a very
This might seem like a very easy answer but I cant seem to wrap
This might seem like a silly question, but after asking some questions on stackoverflow
This may seem like a very simple question, however I haven't been able to
Ok sorry this might seem like a dumb question but I cannot figure this
This might seem like a weird question, but bear with me. I'd like to
This might seem like a trivial question, but I'm a bit muddled in my

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.