Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In addition to accept an input of no more than 5000 characters, You may assume t

ID: 645055 • Letter: I

Question

In addition to accept an input of no more than 5000 characters,

You may assume that no word in the input will have more than 25 characters. Either a space or a tab character ( ) will separate words. Write a c program to accept an input and a integer number j. Now, transpose the odd and even words in the input and then apply the Caesar cipher with a right shift of j to transpose the text. Print out the encrypted text.

Given the sentence:

"The night sky is very black"

when the odd and even words are transposed, it would read

"night The is sky black very"

You would then apply a Caesar Shift on the transposed sentence.

Here is my code for the caesar cipher if it helps.


#include

void encrypt(int shift);

int main(void)
{
   int shift;
   int decoding;
  
   printf("Amount of shifts: ");
   scanf("%d", &shift);
   if (shift < 0)
   {
       printf("Bad Input.");
       return 0;
   }
   printf("Type 1 to encrypt or 0 to decrypt: ");
   scanf("%d", &decoding);
   if (decoding != 0 && decoding !=1)
   {
       printf("Bad Input.");
       return 0;
   }
   while(getchar() != ' ');
   if (decoding == 1)
       encrypt(shift);
   else
   {
       shift = -1 * shift;
       encrypt(shift);
   }
   return 0;
}

void encrypt(int shift)
{
   char ch;
   printf("Please enter a string: ");
   ch = getchar();
   while(ch != ' ')
   {
       if (ch == ' ')
           putchar(ch);
       else
       {
           if(shift == 1)
               putchar(ch + shift);
           else
               putchar(ch - shift);
       }
       ch = getchar();
   }
   putchar(ch);
}

Explanation / Answer

/*Function to reverse words*/
void reverseWords(char *s)
{
char *word_begin = s;
char *word_begin1=s;
char *temp = s; /* temp is for word boundry */
int cnt=0;
while( *temp )
{
temp++;
if (*temp == '')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' '&& cnt== 1 )
{
reverse(word_begin, temp-1);
reverse(word_begin1,temp-1);
word_begin1 = temp+1;
word_begin = temp+1;
cnt =0;
}
else if(*temp == ' ' && cnt == 0)
{
reverse(word_begin, temp-1);
word_begin = temp+1;
cnt=1;
}
} /* End of while */
reverse(word_begin1,temp-1);

}

/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}

/* Driver function to test above functions */
int main()
{
char s[] = "i like this program very much";
char *temp = s;
int shift;
int decoding;

printf("%s", s);
printf("Amount of shifts: ");
scanf("%d", &shift);
if (shift < 0)
{
printf("Bad Input. ");
return 0;
}
printf("Type 1 to encrypt or 0 to decrypt: ");
scanf("%d", &decoding);
if (decoding != 0 && decoding !=1)
{
printf("Bad Input.");
return 0;
}
if (decoding == 1)
{

reverseWords(s);
encrypt(shift,s);
}
else
{
shift = -1 * shift;
reverseWords(s);
encrypt(shift,s);
}
return 0;
}
void encrypt ( int shift, char cipher[]) {
int i = 0;

while (tolower(cipher[i]) != '') {
if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) {
cipher[i] += (shift);
} else {
cipher[i] += (shift - 25);
}
i++;
}
printf("%s", cipher);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote