-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE13101caser.c
74 lines (68 loc) · 1.95 KB
/
E13101caser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
//prototyping functions
int rotateright(int ch);
int rotateleft(int ch);
int encode(int ch,int shift);
int main(){
int shift;
int ch=0;
char text[200]; //declaring array size of 200
printf("Enter shift: ");
scanf("%d",&shift);
printf("Enter text: \n");
int i=0;
while(ch!= -1){
ch= getchar();
text[i]=ch; //get ASCII value of the charater into array text
i++;
}
printf("\nHere is the encoded text: ");
int a =i-1;
for(i=0;i<a;i++){
if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z')){ //checking charachters within this range(only letters)
if(shift != 0){
text[i]=encode(text[i],shift); //calling encode function
}
}
printf("%c",text[i]); //printing text
}
printf("\n");
return 0;
}
// creating rotateright function
int rotateright(int ch){
if((ch>=65&& ch<90) || (ch>=97 && ch<122)){ //checking letters without 'A' and 'a'
ch = ch+1;
}else if(ch==90){ //checking character 'A'
ch='A';
}else{
ch='a'; //checking character 'a'
}
return ch;
}
//creating rotateleft function
int rotateleft(int ch){
if((ch>65&& ch<=90) || (ch>97 && ch<=122)){ //checking letters without 'Z' and 'z'
ch = ch-1;
}else if(ch==65){ //checking character 'Z'
ch='Z';
}else{
ch='z'; //checking character 'z'
}
return ch;
}
//creating encode function
int encode(int ch,int shift){
int i=0;
if(shift>0){ //checking only positive shift numbers
for(i=0;i<shift;i++){
ch=rotateright(ch); //calling rotateright function
}
}else{
for(i=0;i>shift;i--){ //checking negative shift numbers
ch=rotateleft(ch); //calling rotateleft function
}
}
return ch;
}