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

C PROGRAMMING! (NOT C++) Write the following functions, consistent with the prot

ID: 3679609 • Letter: C

Question

C PROGRAMMING! (NOT C++)

Write the following functions, consistent with the prototypes shown: Given a string, return the number of characters in the string, NOT INCLUDING the null at the end. The function will copy the string s2 into the string s1. Note the function returns the address of the string. The function concatenates the string s2 into the end of string s1. If s1 is "Hello " and s2 is "World" before, after calling strcat, s1 will be "Hello World" Important: Do NOT use the C library routines. Do implement the functions EXACTLY as prototyped (i.e., no extra parameters, no fewer parameters, etc.) You may use the functions as "building blocks". Write main() functions to test and debug each of the above string functions, using gets() and puts() for string input and output as follows: To demonstrate strlen(), input your first name using gets() and use printf to print the string followed by the number of characters in the string. An input of "Ken" would print 3. Show this with 3 different string input examples. To demonstrate strcpy(), input your first name using gets(s1) and use strcpy() to copy it to a second string, s2 and print both strings with puts(). So an input of "Ken" would print: "stringl = Ken string2 = Ken". To demonstrate strcat(), use gets() to input your first name, last name, and ID as three separate strings, print them out, one per line, concatenate the three strings, and then use puts() to print out Hello followed by your name and ID. If the three input strings entered are "Ken" "Arnold" and "876543210", then it should print:

Explanation / Answer

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

int strlen(char *s);
char* strcpy(char *s1, char *s2);
char* strcat(char* s1, const char* s2);


int main(){
  
   char* s1 = "Ken";
   char* s2 = "Arnold";
   char* s3 = "876543210";
   char* s4 = (char*)malloc(sizeof(char));
  
   s4 = strcpy(s4, "Hello");
  
   s4 = strcat(s4, " ");
  
   printf("%s ",s4);
   printf("Size: %d ",strlen(s4));
  
   s4 = strcat(s4, s1);
   s4 = strcat(s4, " ");
  
   printf("%s ",s4);
   printf("Size: %d ",strlen(s4));
  
   s4 = strcat(s4, s2);
   s4 = strcat(s4, " ");
  
   printf("%s ",s4);
   printf("Size: %d ",strlen(s4));
  
   s4 = strcat(s4, s3);
   s4 = strcat(s4, " ");
  
   printf("%s ",s4);
   printf("Size: %d ",strlen(s4));
  
   return 0;
}

int strlen(char *s)
{
char *p=s;

   while(*p!='')
   p++;

   return(p-s);
}

char* strcpy(char *s1, char *s2)
{
char *temp = s2;
while((*s2++=*s1++) != '');
return temp;
}

char* strcat(char* s1, const char* s2)
{
int i,j;

for (i = 0; s1[i] != ''; i++);

for (j = 0; s2[j] != ''; j++)
       s1[i+j] = s2[j];

s1[i+j] = '';

return s1;
}

/*

Output:

Hello
Size: 6
Hello Ken
Size: 10
Hello Ken Arnold
Size: 17
Hello Ken Arnold 876543210
Size: 27

*/