Question

In: Computer Science

In C programming. If a grammatically correct input is entered, the program should convert the two...

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'

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in C with code and output explained.
  • For better understanding please read the comment mentioned in the code below.
  • CODE:

//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;
}

  • INPUT/OUTPUT:
  1. abc+bbc
    'abc' + 'bbc' => 'bce'
  2. tuvwx+CBAzy
    'tuvwx' + 'CBAzy' => 'aaaaa'
  3. orange+white
    'orange' + 'white' => 'KyiGkE'
  4. abcqH*AUydkzAB
    'abcqH' * 'AUydkzAB' => 'aUbbbZab'
  5. yyyyyyyyyyyy*abcdefghijkl
    'yyyyyyyyyyyy' * 'abcdefghijkl' => 'aybzcAdBeCfD'
  6. abcqHU/Aabdk
    'abcqHU' / 'Aabdk' => 'aacviu'
  7. bbbbb/ABCDU
    'bbbbb' / 'ABCDU' => 'MhQnU'
  • For better clarity and understanding below are the screenshots attached for the code and input/output.

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.


Related Solutions

Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
"This C Programming " Write a program that accepts four (4) lines of input: • The...
"This C Programming " Write a program that accepts four (4) lines of input: • The first line contains a float value in 4.2f format • The second line contains a char value • The third line contains a 4-digit int value • The fourth line contains a char value and then displays all of the input on a single line Write a program that accepts a phone number of the form +1(xxx)-xxx-xxxx where x is a digit, and displays...
New to C programming and I am stuck. The program will print "no input" if no...
New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly? #include <stdio.h> #include <stdlib.h> int main () { char input; scanf("%c", &input); if (input == NULL)...
C++: Write a program to convert from gallons to liters. The program should allow the user...
C++: Write a program to convert from gallons to liters. The program should allow the user to input the amount with decimal places and then see the result on the screen.
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include<iostream> #include<string> #include<exception> #include <cstdlib> #include<stdio.h> #include<map> #include <cctype> #include<Windows.h> #define INPUT_SIZE 8 using namespace std;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT