In: Computer Science
In C programming. If a grammatically correct input is entered,
the program should convert the two
strings to two integer arrays and perform the given operation on
each array location.
For all operations, consider each corresponding array position
independently (e.g.,
there are no carries). The resulting array should then be converted
back to a string,
and finally printed. If the two input strings are not the same
length, then each output
character beyond the length of the shorter string should be a copy
of the character
from the longer string with the opposite case.
The mathematical operations work as follows. Each symbol should be
converted to
an integer as shown in the table to the right (i.e., a is 0, b is
1, c is 2, …, U is 46).
This mapping forms a finite field, and we can define addition and
multiplication on
this field.
Symbol Value
a 0
b 1
... ...
z 25
A 26
B 27
... ...
T 45
U 46
Page 2 of 3
Addition on a finite field: +
For this operation, for each array position find the sum of the two
integers and if the result is larger than
46 find the remainder when the quotient is 47. That is, find (? +
?) ??? 47. For example, if the user
enters:
> abc+bbc
the result that should be printed is
'abc' + 'bbc' => 'bce'
> tuvwx+CBAzy
'tuvwx' + 'CBAzy' => 'aaaaa'
For this example the first string is longer than the second (the
'e' in orange). The output corresponding
to this extra symbol is 'E':
> orange+white
'orange' + 'white' => 'KyiGkE'
Multiplication on a finite field: *
This operation is handled the same way as addition but uses modular
multiplication. That is, find
(? × ?) ??? 47.
> abcqH*AUydkzAB
the result that should be printed is
'abcqH' * 'AUydkzAB' => 'aUbbbZab'
> yyyyyyyyyyyy*abcdefghijkl
the result that should be printed is
'yyyyyyyyyyyy' * 'abcdefghijkl' => 'aybzcAdBeCfD'
Inversion on a finite field: /
For this operation, given two numbers X / Y, find a number Z such
that ? = (? × ?) ??? 47. However,
if Y is zero, then this operation is undefined, so set the output
to zero.
> abcqHU/Aabdk
the result that should be printed is
'abcqHU' / 'Aabdk' => 'aacviu'
> bbbbb/ABCDU
the result that should be printed is
'bbbbb' / 'ABCDU' => 'MhQnU'
//include headers
#include<stdio.h>
#include<string.h>
//take mod value as 47
const int mod=47;
//function to multiply two numbers and take mod by 47 and return
the value
int mul(int a, int b){
return (a*b)%mod;
}
//function to calculate the (b/a)%mod
int modInverse(int a, int b){
//base case
if(a==1){
return b;
}
//recur
int x=mod/a+1;
return modInverse(mul(a,x),mul(b,x));
}
//function to calculate maximum of a and b
int max(int a,int b){
if(a<b)return b;
else return a;
}
//function to calculate minimum of a and b
int min(int a,int b){
if(a<b)return a;
else return b;
}
//driver function
int main(){
//input character array of max size 100 you can increase if input
is bigger than 100 length
char inp[100];
//take input
scanf("%s",inp);
//length of input array
int n=strlen(inp);
//variables to check if the operation is addition , multiplication
or division
int addition=0,multiplication=0,division=0;
//length variable to store length of both strings
int len1=0,len2=0;
//traverse the character array
for(int i=0;i<n;i++){
//check for the operation and set the corresponding variable to 1
and break from loop
//also save length of string 1 as it is till the character just
before the operation character
if(inp[i]=='*'){
len1=i;
multiplication=1;
break;
}
if(inp[i]=='+'){
len1=i;
addition=1;
break;
}
if(inp[i]=='/'){
len1=i;
division=1;
break;
}
}
//calculate the length of second string
len2=n-len1-1;
//create a character array to store result
char result[max(len1,len2)];
//if operation is addition
if(addition==1){
int i;
//traverse till the length of minimum of two string and calculate
the resulting character after applying the operation
for(i=0;i<min(len1,len2);i++){
int x;
//x stores the value of first string's character
if(inp[i]<='z' && inp[i]>='a'){
x=inp[i]-'a';
}
else{
x=inp[i]-'A';
x=x+26;
}
int y;
//y stores the value of second string's character
if(inp[i+len1+1]<='z' && inp[i+len1+1]>='a'){
y=inp[i+len1+1]-'a';
}
else{
y=inp[i+len1+1]-'A';
y=y+26;
}
//calculate final answer for this character
int z=x+y;
z=z%47;
//convert it back to the corresponding character
if(z<=25){
result[i]=(char)('a'+z);
}
else{
z=z-26;
result[i]=(char)('A'+z);
}
}
//create result for the longest string in the given both
while(i<len1){
char ch=inp[i];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
while(i<len2){
char ch=inp[i+len1+1];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
}
//if operation is multiplication
else if(multiplication==1){
int i;
//traverse till the length of minimum of two string and calculate
the resulting character after applying the operation
for(i=0;i<min(len1,len2);i++){
int x;
//x stores the value of first string's character
if(inp[i]<='z' && inp[i]>='a'){
x=inp[i]-'a';
}
else{
x=inp[i]-'A';
x=x+26;
}
int y;
//y stores the value of second string's character
if(inp[i+len1+1]<='z' && inp[i+len1+1]>='a'){
y=inp[i+len1+1]-'a';
}
else{
y=inp[i+len1+1]-'A';
y=y+26;
}
//calculate final answer for this character
int z=mul(x,y);
//convert it back to the corresponding character
if(z<=25){
result[i]=(char)('a'+z);
}
else{
z=z-26;
result[i]=(char)('A'+z);
}
}
//create result for the longest string in the given both
while(i<len1){
char ch=inp[i];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
while(i<len2){
char ch=inp[i+len1+1];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
}
//if operation is division
else if(division==1){
int i;
//traverse till the length of minimum of two string and calculate
the resulting character after applying the operation
for(i=0;i<min(len1,len2);i++){
int x;
//x stores the value of first string's character
if(inp[i]<='z' && inp[i]>='a'){
x=inp[i]-'a';
}
else{
x=inp[i]-'A';
x=x+26;
}
int y;
//y stores the value of second string's character
if(inp[i+len1+1]<='z' && inp[i+len1+1]>='a'){
y=inp[i+len1+1]-'a';
}
else{
y=inp[i+len1+1]-'A';
y=y+26;
}
//calculate final answer for this character
int z=0;
//if y is not 0 then only division % mod is performed otherwise
result for this is 0
if(y!=0){
z=modInverse(y,x);
}
//convert it back to the corresponding character
if(z<=25){
result[i]=(char)('a'+z);
}
else{
z=z-26;
result[i]=(char)('A'+z);
}
}
//create result for the longest string in the given both
while(i<len1){
char ch=inp[i];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
while(i<len2){
char ch=inp[i+len1+1];
if(ch<='z' && ch>='a'){
result[i]=(char)(ch-'a'+'A');
}
else{
result[i]=(char)(ch-'A'+'a');
}
i+=1;
}
}
//printing the result in the format given
//for first string
printf("'");
int i;
for(i=0;i<len1;i++){
printf("%c",inp[i]);
}
printf("' %c '",inp[len1]);
i+=1;
//for second string
for(;i<n;i++){
printf("%c",inp[i]);
}
printf("' => '");
//for resulting string
for(int j=0;j<max(len1,len2);j++){
printf("%c",result[j]);
}
printf("'");
return 0;
}
CODE
INPUT/OUTPUT
So if you have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.