-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
124 lines (96 loc) · 2.4 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
char encodeRot13(char c);
int indexOf(char c, char* s);
int stringLength(char* s);
char* encodeRot13String(char* s);
int main()
{
// ftok to generate unique key
key_t key = ftok("shmfile",65);
printf("%d\n", IPC_CREAT);
// shmget returns an identifier in shmid
int shmid = shmget(key,1024,0666|IPC_CREAT);
// shmat to attach to shared memory
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Write Data : \n");
scanf("%s", str);
sprintf(str, encodeRot13String(str));
//str = encodeRot13String(str);
//printf("%s", encodeRot13String(str));
//scanf("%s", str);
printf("Data written in memory: %s\n",str);
//detach from shared memory
sprintf(str, str);
shmdt(str);
return 0;
}
char* concatenate(char* str1, char* str2)
{
int str1Length = stringLength(str1);
int str2Length = stringLength(str2);
int totalLength = str1Length + str2Length;
char* ans = (char*)malloc(totalLength * (int)sizeof(char));
for(int i = 0; i < totalLength; i++)
{
if(i < str1Length)
{
ans[i] = str1[i];
}
else
{
ans[i] = str2[i - str1Length];
}
}
return ans;
}
int stringLength(char* s)
{
int count = 0;
while(*s != '\0')
{
count++;
s = s + 1;
}
return count;
}
char encodeRot13(char c)
{
char* alphabet = "abcdefghijklmnopqrstuvwxyz";
int index = indexOf(c, alphabet);
//printf("%d\n", index);
index = (index + 13)%26; //17 + 13 = 30
//printf("%d\n", index);
//return alphabet[index];
return *(alphabet + index);
//how did we tell if we wrapped around?
//**********START HERE!!!!!
}
int indexOf(char c, char* s)
{
int s_length = stringLength(s);
for(int i = 0; i < s_length; i++)
{
if(c == *(s+i))
{
return i;
}
}
return -1;
}
char* encodeRot13String(char* s)
{
//how big will my output be?
int length = stringLength(s);
char* answer = (char*)malloc(length * (int)sizeof(char));
for(int i = 0; i < length; i++)
{
char* charToCat = (char*)malloc(1);
charToCat[0] = encodeRot13(*(s + i));
//char charToCat = encodeRot13(s + i);
answer = concatenate(answer, charToCat);
}
return answer;
}