1

I'm writing a code in C programming language that receives a string (of chars) as input and each letter advances 3 in the alphabet. E.g. If user types "abc", the program should return "def". The problem is that if the user types 'z' (e.g.), the program returns some char, instead of my goal (which would be this case the letter 'c'). My current algorithm includes this if statement:

if ((text[i]>='a' && text[i]<='w')||(text[i]>='A' && text[i]<='W'))
                  text[i] = (text[i]) + 3;

But this forces me to write all this lines:

       else if (text[i]=='x') text[i]='a';
            else if (text[i]=='X') text[i]='A';
                 else if (text[i]=='y') text[i]='b';
                      else if (text[i]=='Y') text[i]='B';
                           else if (text[i]=='z') text[i]='c';
                                else if (text[i]=='Z') text[i]='C';

How can I optimize my code?

8
  • 3
    Sounds like you need % (modulo/remainder). Commented May 2, 2015 at 22:52
  • @JoachimPileborg, the thing is that when the program tries to convert some x, y or z, he returns the next char with the next ASCII code, and not starts again over the alphabet, as I wish... That means that if I type a z he possibly returns some punctuation sign or other symbols... Commented May 2, 2015 at 22:55
  • @EOF , how could I use the % ? Commented May 2, 2015 at 22:55
  • 1
    for capital ASCII: newchar = 'A'+((oldchar-'A'+offset)%(number_of_capital_chars));. offset is 3 in your case. Commented May 2, 2015 at 22:59
  • or a pair of doubled-up alphabet arrays if for some reason you're averse to the modulo operator. There are a multitude of ways to do this besides a stack of single-value if-else-if. Commented May 2, 2015 at 23:03

1 Answer 1

1

Your problem can be addressed with simple arithmetic logic. The range of char ranges from 0 - 255. Each value corresponds to a separate character. The letters 'A-Z' and 'a-z' range from 65 - 90 and 97 - 122 respectively. So for your problem there are two ranges. You can check with the standard library function that your character falls in upper case or lower case range. then you can set the base for your range. Next you will find the offset of your character from base, add 3 in it. the new value can be made circular using % operator. each range has maximum of 26 characters so you can make your range circular by taking a mod from 26. Now add the resulting value (offset) to the base to get the desired character.

#include <ctype.h>
...
...
...

char c = text[i];
char base = isupper(c)? 'A' : 'a';
text[i] = base + (((c - base) + 3) % 26);    // New Desired Character
Sign up to request clarification or add additional context in comments.

1 Comment

Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.