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

Your job is to write several C functions performing operations on strings and st

ID: 3825576 • Letter: Y

Question

Your job is to write several C functions performing operations on strings and structures and to write test programs for each function by calling them with different test values.

Write a C program pattern.c that includes a main program and a function with the following function prototype:

int contains ( const char *text, const char *pattern );

The main program must test the function by calling it with 5 different values for text and pattern and printing the results to the screen using the following format string:

"Pattern %s occurs in %s at the location %d. "

The function accepts two text strings, text and pattern as input. It then determines the location of the specified pattern string in the text string. If the pattern occurs multiple times, it returns the first occurrence of the pattern and if it does not occur, then the function returns -1. For example, if called with the text “Hello, World!” and the pattern “orl”, the function returns 8 because the pattern starts at position 8 in the text string. (25 points)

            Sample Output:

Write a C program coding.c that includes a main program and two functions with the following function prototypes:

void encodeStr ( char *input, char *output, CodeT code );

void decodeStr ( char *input, char *output, CodeT code );

The first function takes the input string in input and generates the encoded string using the information in code. The result is stored in output. The second function takes the input string in input and decodes it back using the information in code. The result is stored in output again.

Both functions use the following structure as the key to encode and decode text messages:

typedef struct {

   char from[28];

   char to[28];

} CodeT;

The main function may then initialize the values as shown below:

CodeT code = {   .from = " abcdefghijklmnopqrstuvwxyz",

                 .to = ".172093%@#+!:_-$^*()854=6?>" };

The main program must prompt the user repeatedly for a line of text and then generate and print the encoded text string before decoding the encoded text string and printing the result again to the screen to test that the original text string can be retrieved through the decoding function. The program should stop prompting the user for input text when the user enters an empty text as input. Use the program discussed in class to read text lines from the keyboard. (25 points)

Sample Output:

Write a C program sort.c that prompts the user to enter the number of words to be sorted and stores them in an array. The program then sorts the words in the array alphabetically and prints the sorted list to the screen. You may use strcmp()available in the “string.h” library to compare two strings alphabetically. The prototype of the sort function must look like:

void sort ( WordT *words, int numWords );

You must use the following type definition:

typedef struct {

   char label[20];

} WordT;

(25 points)

Sample Output:

Submission Requirement:

The project requires 3 source code file to be turned in.

pattern.c (the source code for solving problem1)

coding.c (the source code for solving problem 2)

sort.c (the source code for solving problem 3)

Explanation / Answer

Answer:

Note: Kindly post sort.c as separate question.

File Name: pattern.c

#include <stdio.h>

#include <string.h>

//function returns the position of pattern in text if pattern exist in text

int contains(const char *text, const char *pattern)

{

          //Declare the required variables

          int l1,l2,aa,bb,ps=-1;

          int fg=0;

          //Find len of text and pattern

          l1=strlen(text);

          l2=strlen(pattern);

          //Loop to check for the entire text

          for(aa=0;aa<=l1-l2;aa++)

          {

              //Loop to check the pattern existency

              for(bb=aa;bb<aa+l2;bb++)

              {

                   //update fg and ps

                   fg=1;

                   ps=aa;

                   //if characters are not matching

                   if(text[bb]!=pattern[bb-aa])

                   {

                        //update the fg and ps

                        fg=0;

                        ps=-1;

                        //exit from loop

                        break;

                   }

              }

              // exit

              if(fg==1)

                   break;

          }

          //return ps

          return ps;

}

int main ()

{

     char text[200];

     char pattern[200];

     int kk=0;

     for(kk=0;kk<5;kk++)

     {

         

          printf("Enter the TEXT : ");

          fgets(text, sizeof(text), stdin);

          text[strlen(text)-1]='';

          printf("Enter the PATTERN : ");

          fgets(pattern, sizeof(pattern), stdin);

          pattern[strlen(pattern)-1]='';

          int isAvail = contains(text,pattern);

          if(isAvail != -1) //if isAvail

printf("Pattern %s occurs in %s at the location %d. ",pattern,text,isAvail);

          else

              printf("Pattern not FOUND. ");

     }

     return 0;

}

Sample output:

Enter the TEXT :Hello World

Enter the PATTERN:orl

Pattern orl occurs in Hello World at the location 7.

Enter the TEXT :Hello World

Enter the PATTERN:lo

Pattern lo occurs in Hello World at the location 3.

Enter the TEXT :Hi

Enter the PATTERN:hello

Pattern hello occurs in Hello World at the location -1.

Enter the TEXT :Hello World

Enter the PATTERN:world

Pattern world occurs in Hello World at the location -1.

Enter the TEXT :Study well

Enter the PATTERN:well

Pattern well occurs in Study well at the location 6.

File Name : coding.c

#include <stdio.h>

#include <string.h>

//Declare the CodeT

typedef struct

{

   char from[28];

   char to[28];

}CodeT;

void encodeStr(char *input, char *output, CodeT code);

void decodeStr(char *input, char *output, CodeT code);

//main

int main(void)

{

    

     int kk;

     CodeT code ={ .from = " abcdefghiklmnopqrstuvwxyz",

          .to = ".172093%@#+!:_-$^()854=6?>"

     };

     char inpStrr[100] ;

     char outpStrr[100];

     while(1)

     {

          printf("Enter the text : ");

          fgets(inpStrr, sizeof(inpStrr), stdin);

          if(inpStrr[strlen(inpStrr-1)]==' ')

              break;

          encodeStr(&inpStrr,&outpStrr,code);

          printf(" Decoded String: ");

         for(kk=0;kk<strlen(outpStrr);kk++)

          {   

              printf("%c",outpStrr[kk]);

          }

          printf(" EncodedString: ");

          decodeStr(&outpStrr,&inpStrr,code);

          for(kk=0;kk<strlen(inpStrr);kk++)

          {   

              printf("%c",inpStrr[kk]);

          }

     }

     return 0;

}

void decodeStr(char *input, char *output, CodeT code)

{

     int aa,bb;

     int myLen = strlen(input);

     for(aa=0;aa<myLen;aa++)

     {        

          for(bb=0;bb<28;bb++)

          {

              if(input[aa]==code.to[bb])

              {

                   output[aa] = code.from[bb];

                   break;

              }

          }

     }

     output[aa]='';

}

void encodeStr(char *input, char *output, CodeT code)

{

     int aa,bb;

     int myLen = strlen(input);

     for(aa=0;aa<myLen;aa++)

     {        

          for(bb=0;bb<28;bb++)

          {

              if(input[aa]==code.from[bb])

              {

                   output[aa] = code.to[bb];

                   break;

              }

          }

     }

     output[aa]='';

}

Sample output:

Enter the text : hello

Decoded String:

@9!!-

EncodedString:

hello

Enter the text :