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

C PROGRAM PLEASE Write a program which declares a string and sets it to contain

ID: 3867552 • Letter: C

Question

C PROGRAM PLEASE

Write a program which declares a string and sets it to contain the phrase “Little butterflies listlessly fly among the lilies”. The string is first written to an output file and is then passed to a function called convert which converts each occurrence of “li” to “LI”. The main then writes the converted string to the output file, convert.txt. You program should consist of both the main and the function convert. The output file should look like: The original string is: Little butterflies listlessly fly among the lilies The string now is: Little butterfLIes LIstlessly fly among the LILIes

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>


char *convert(char *str){

     int i;
     char *p = (char *)malloc(sizeof(char) * 100);

     i = 0;
     while (*(str + i) != ''){
         if (*(str + i) == 'l' && *(str + i + 1) == 'i'){
            *(p + i) = 'L';
            *(p + i + 1) = 'I';
            i = i + 2;
         }
         else {
             *(p + i) = *(str + i);
             i++;
         }
        
     }
     return p;
}


int main(){

   FILE *fp;
   char *str = "Little butterflies listlessly fly among the lilies";
  
   fp = fopen("convert.txt", "w");
   fprintf(fp, "%s ", str);
   fclose(fp);
   str = convert(str);
   fp = fopen("convert.txt", "a");
   fprintf(fp, "%s ", str);
   fclose(fp);

  
}