Home > OS >  C program that sums a char with int
C program that sums a char with int

Time:12-17

I have a given exercise that wants me to find the uppercase letter that is K places from the letter in this case char variable that is named C. The range is uppercase letters from A to Z.

For example if the input is B 3 the output should be E. For this specific input its simple you just sum the values and you get your answer but for example what if we go out of the range. Here is one example F 100 the program should output B because if the value is > than Z the program starts from A.

If there are some confusions I will try to explain it more here are some test cases and my code that only work if we don't cross the range.

Input    Output
B 3        E
X 12345    S
F 100      B
T 0        T
#include <stdio.h>

int main(){
    int K;
    char C,rez;
    scanf("%c %d",&C,&K);

    int ch;
    for(ch = 'A';ch <= 'Z';ch  ){
           if(C>='A' && C<='Z'){
               rez = C K;
           }
    }

    printf("%c",rez);
    return 0;
}

CodePudding user response:

Think of the letters [A-Z] as base 26 where zero is A, one is B and 25 is Z.

As we sum of the letter (in base 26) and the offset, it is only the least significant base 26 digit we have interest, so use % to find the least significant base 26 digit much like one uses % 10 to find the least significant decimal digit.

scanf(" %c %d",&C,&K);
//     ^ space added to consume any white-space

if (C >= 'A' && C <= 'Z') {
  int base26 = C - 'A';
  base26 = base26   K;
  base26 %= 26;
  int output = base26   'A';
  printf("%c %-8d %c\n", C, K, output);
}

For negative offsets we need to do a little more work as % in not the mod operator, but the remainder. This differs with some negative operands.

  base26 %= 26;
  if (base < 0) base26  = 26; // add
  int output = base26   'A';

Pedantically, C K may overflow with extreme K values. To account for that, reduce K before adding.

  // base26 = C   K;
  base26 = C   K&;

We could be a little sneaky and add 26 to insure the sum is not negative.

if (C >= 'A' && C <= 'Z') {
  int base26 = C - 'A';
  base26 = base26   K&   26; // base26 >= 0, even when K < 0
  base26 %= 26; // base26 >= 0 and < 26
  int output = base26   'A';
  printf("%c %-8d %c\n", C, K, output);
}

... or make a complex one-line

  printf("%c %-8d %c\n", C, K, (C - 'A'   K&   26)&    'A');

CodePudding user response:

This can be accomplished by using 2 concepts.

  1. ASCII value
  2. Modulus operator (%)

In C every character has an ASCII value. Basically it goes from 0-127. The character 'A' has the value of 65 The character 'B' has the value of 66 (65 1) and so on... Until Z which is 65 25 = 90

And the 2nd concept I want to highlight in math is modulo arithmetic where if you always want to map a number to certain range, you can use a modulus operator. Modulus is the reminder that you get after dividing a number by another number. In our case, we have 26 alphabets so we can always get a number between 0 to 25

For the example you took 100 % 26 = 22 But you have to consider the starting point too.

So, we always subtract the initial alphabet by the value of 'A', i.e. 65 so that 'A' maps to 0 and 'Z' maps to 25

So, if we start with 'F' and need to go 100 places..

  1. Subtract 'A' value from 'F' value. Characters behave like numbers so you can actually store 'F' - 'A' in an integer

In this case 'F' - 'A' = 5

  1. Next we add the offset to this. 5 100 = 105

  2. Then we perform modulus with 26 105 % 26 = 1

  3. Finally add the value of 'A' back to the result 'A' 1 = 'B'

And you are done

CodePudding user response:

Get the remainder of input number with 26 using modulo operator. If sum of input character and remainder is less than or equal to Z then its the answer otherwise again find the remainder of sum with 26 and that will be answer (take care of offset because the ASCII decimal value of letter A is 65).

Roughly the implementation will be:

#include <stdio.h>

int main(){
    int K;
    char C, rez;
    scanf("%c %d",&C,&K);
    // Validate the user input

    int ch;

    int rem = K % 26;

    if ((rem   C) - 'A' < 26) {
        rez = rem   C;
    } else {
        rez = ((rem   C - 'A') % 26)   'A';
    }
    
    printf("%c\n",rez);

    return 0;
}

Note that, I know there is scope of improvement in the implementation. But this is just to give an idea to OP about how it can be done.

Output:

# ./a.out
B 3
E
# ./a.out
X 12345
S
# ./a.out
F 100
B
# ./a.out
T 0
T
  • Related