Write a function \(\texttt{char encrypt (char c, int k)}\) that encrypts a character as follows: if c is an alphabetic character, shift c by k to the right (+ k) otherwise shift c by k to the left (-k).

Write a main function that tests the function.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>

char encrypt (char c, int k)
{
    if(c>='a' && c<='z' || c>='A' && c<='Z' )
        return c+k;
    return c-k;
}

int main()
{
    int i;
    char str[]="ABcDe0123";
    for(i=0;str[i];i++)
        printf("%c",encrypt(str[i],2));
	
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Set of integer in binary search trees