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

DUE TODAY AT 11:59 THIS IS A C PROGRAM THIS IS THE CODE WE DID IN CLASS I APPREC

ID: 3735014 • Letter: D

Question

DUE TODAY AT 11:59 THIS IS A C PROGRAM

THIS IS THE CODE WE DID IN CLASS I APPRECIATE ANY HELP

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(int argc, char **argv) {

int i, j, howMany;

  

char **wordList;

char* tmp;

printf("How many words in the list bruh? ");

scanf("%d",&howMany);

wordList = malloc( howMany * sizeof(char*));

  

for(i=0;i<howMany;i++) {

wordList[i] = malloc(51);

scanf("%50s", wordList[i]);

  

}

for(i=0;i<howMany; i++) {

for(j=0; j<howMany-1; j++) {

if (strcmp(wordList[j],wordList[j+1]) > 0) {

tmp = wordList[j];

wordList[j] = wordList[j+1];

wordList[j+1] = tmp;

}

}

}

printf("--------------------------------------------------------- ");

for(i=0; i<howMany; i++) {

printf("%s ", wordList[i]);

}

free(wordList);

}

Program #1: GS . o Modify the sorting list of words example from class in the following ways " Instead of single words, read pairs of first/last name pairs Sort full names based on last name, then sort based on first name if the last names are identical tip use two arrays of pointers to strings(arrays of char) o one for the first names o one for the last names * when sorting, you just need to perform the swap on both lists " Take a command line argument as a filename, and get the list of names from that file " Take a second command line aumen as a filename, and write the sorted list of name to a file of that name " Make the sorting case-insensitive, by creating & using your own version of the library function strcmp o BUT... when printing the names, you should keep the original capitalization Function name: strcmp.ci (stands for "string compare, case insensitive") * parameters and return value work just like strcmp, except must be case-insensitive * tips O instead of comparing names directly, copy them to temporary char arrays convert letters in the temporary array to either call uppercase or all lowercase compare the the temporary arrays to decide whether two words need to be swapped

Explanation / Answer

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

int strcmpi(char *p, char *q){
     int i;
     char *r = (char *)malloc(sizeof(char)*strlen(p));
     char *s = (char *)malloc(sizeof(char)*strlen(q));
     strcpy(r,p);
     strcpy(s,q);   
     for (i = 0; i<strlen(p); i++)
         r[i] = tolower(r[i]);
     for (i = 0; i<strlen(q); i++)
         s[i] = tolower(s[i]);

     return strcmp(r,s);
}

int main(int argc, char *argv[]){

   FILE *fp1, *fp2;
   char first[100][50];
   char last[100][50];
   char temp1[50];
   char temp2[50];
   int i, j;

   fp1 = fopen(argv[1],"r");
   if (fp1 == NULL){
      printf("Error opening file ");
      return 0;
   }
   int count = 0;
   while (!feof(fp1)){
       fscanf(fp1,"%s%s", first[count],last[count]);
       count++;
   }
   for (i = 0; i<count; i++){
       for (j = i; j<count; j++){
           if (strcmpi(last[i], last[j]) > 0){
               strcpy(temp1,last[i]);
               strcpy(temp2,first[i]);
               strcpy(last[i],last[j]);
               strcpy(first[i],first[j]);  
               strcpy(last[j],temp1);
               strcpy(first[j],temp2);              
           }
           if (strcmpi(last[i], last[j]) == 0){
              if (strcmpi(first[i],first[j]) > 0){
                 strcpy(temp1,last[i]);
                 strcpy(temp2,first[i]);
                 strcpy(last[i],last[j]);
                 strcpy(first[i],first[j]);  
                 strcpy(last[j],temp1);
                 strcpy(first[j],temp2);              

              }
           }

       }
   }
   fp2 = fopen(argv[2],"w");
   for (i = 0; i<count; i++){
       fprintf(fp2, "%s %s ",first[i],last[i]);
   }
   fclose(fp2);    
   return 0;
}