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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:25:33+00:00 2026-05-25T21:25:33+00:00

I am making a Caesar cipher and I want to make the letters in

  • 0

I am making a Caesar cipher and I want to make the letters in a loop so for example if the letter ‘z’ needs to be shifted it should go back to ‘a’ for both capital and lowercase.

//Array that holds each char in the plaintext inputed is declared and initiated
char[] chars = plainTextInput.ToCharArray();

//For loop that will go through each letter and change the value of each letter by adding the shiftAmount
for (int i = 0; i < plainTextInput.Length; ++i)
{   
    chars[i] = (char)(((int)chars[i]) + shiftAmount);

    if (chars[i] >= 97 && chars[i] <= 122)
    {
        if (chars[i] > 122)
        {
            int x = chars[i] - 123;
            chars[i] = (char)((int)(97 + x));
        }
    }
}  

//Variable for ciphertext output holds char array chars as a string
cipherTextOutput = new string(chars); 

If I input 'xyz' and shift by one I get 'yz{'.

  • 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-05-25T21:25:34+00:00Added an answer on May 25, 2026 at 9:25 pm

    Use modulo arithmetic:

    new_pos = (current_pos + shift) % 26
    

    current_pos has to be the relative letter position (eg: a=0, b=1... z=25). Something like:

    if ('A' <= c && c <= 'Z')      // uppercase
    {
        current_pos = (int) c - (int) 'A';
    }
    else if ('a' <= c && c <= 'z') // lowercase
    {
        current_pos = (int) c - (int) 'a';
    }
    

    See working demo: http://ideone.com/NPZbT


    That being said, I hope this is just code you are playing with, and not something to be used in real code.

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

Sidebar

Related Questions

Making a ternary logic table, and I would like to make my own function
Making a left hand side menu for my website. Should I create a Unordered
making an infinite loop in javascript, and jQuery... this is my current code: $(#bg2).css({opacity:
Making an app where we want to send a photo to a server along
Making an argument parser. I want to split a string into an array where
Im making a system which should be running 24/7, with timers to control it.
Making echo of a question around the web: Is the syntax for svn:ignore patterns
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making UML sequence diagram in VS 2010RC I've observed that there is no activation

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.