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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:13:19+00:00 2026-05-15T11:13:19+00:00

I need to code a method that increment a string value from AAA to

  • 0

I need to code a method that increment a string value from AAA to ZZZ with cyclic rotation (next value after ZZZ is AAA)

Here is my code:

    public static string IncrementValue(string value) {
        if (string.IsNullOrEmpty(value) || value.Length != 3) {
            string msg = string.Format("Incorrect value ('{0}' is not between AAA and ZZZ)", value);
            throw new ApplicationException(msg);
        }
        if (value == "ZZZ") {
            return "AAA";
        }
        char pos1 = value[0];
        char pos2 = value[1];
        char pos3 = value[2];

        bool incrementPos2 = false;
        bool incrementPos1 = false;

        if (pos3 == 'Z') {
            pos3 = 'A';
            incrementPos2 = true;
        } else {
            pos3++;
        }

        if (incrementPos2 && pos2 == 'Z') {
            pos2 = 'A';
            incrementPos1 = true;
        } else {
            if (incrementPos2) {
                if (pos2 == 'Z') {
                    pos2 = 'A';
                    incrementPos1 = true;
                }
                pos2++;
            }
        }

        if (incrementPos1) {
            pos1++;
        }

        return pos1.ToString() + pos2.ToString() + pos3.ToString();
    }

I know this piece of code is quite dirty and not very efficient but I dont know how to do it properly.

How is secured this snippet?
(this will only run on windows plaform)

How can I optimize-it and make it more readable ?

Thanks for your comments

  • 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-15T11:13:20+00:00Added an answer on May 15, 2026 at 11:13 am

    Think about it mathematically: Your strings (AAA, AAB, …) behave just like natural numbers (000, 001, …), with the exception of being base 26 instead of base 10.

    So, you can use the same principle. Here is some code:

    // iterate cyclicly from 0 to 26^3 - 1
    int incrementValue(int i) {
        // a verbose way of writing "return (i + 1) % 26^3"
        i++;
        if (i == 26*26*26) i = 0;
        return i;
    }
    
    // convert 0 to AAA, 1 to AAB, ...
    string formatValue(int i) {
        var result = new StringBuilder();
    
        result.Insert(0, (char)('A' + (i % 26)));
        i /= 26;
        result.Insert(0, (char)('A' + (i % 26)));
        i /= 26;
        result.Insert(0, (char)('A' + (i % 26)));
    
        return result.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is there a philosophical reason why the constructor can't support… May 16, 2026 at 10:35 am
  • Editorial Team
    Editorial Team added an answer CSS Usage is a great Firefox add-in. You can browse… May 16, 2026 at 10:35 am
  • Editorial Team
    Editorial Team added an answer One suggestion is to create a database view for your… May 16, 2026 at 10:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I had a need for a method that could take a collection of strings,
I've just written a method that is called by multiple threads simultaneously and I
I need to create an extension method to array class, but this extension method
We have a project that is starting to get large, and we need to
First I would like to say that I have only worked with java for
I have a table that contains a column called 'id' that is an INT
Here's the setup: I'm trying to make a relatively simple Winforms app, a feed
I am new to threads and in need of help. I have a data
I have a set of classes that were created by www.sudzc.com (awesome WDSL web
I have a client/server question that i am trying to figure out the best

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.