In: Computer Science
Please read the Specifications carefully. Also do not use any C library.
Program Specifications
Assuming that all input strings are non-empty (having length of at least 1) and shorter than MAX_LENGTH, implement the following string functions: • strgLen( s ): return the length of string s. • strgCopy( s, d ): copy the content of string s (source) to d (destination). • strgChangeCase( s ): for each character in the string, if it is an alphabet, reverse the case of the character (upper to lower, and lower to upper). Keep the non-alphabet characters as is. • strgDiff( s1, s2 ): compare strings s1 and s2. Return the index where the first difference occurs. Return -1 if the two strings are equal. • strgInterleave( s1, s2, s3 ): copy s1 and s2 to s3, interleaving the characters of s1 and s2. If one string is longer than the other, after interleaving, copy the rest of the longer string to s3. For example, given s1 = “abc” and s2 = “123”, then s3 = “a1b2c3”. If s1 = “abcdef” and s2 = “123”, then s3 = “a1b2c3def”. Notes: • Do not use any C library function at all in your code. Do not add any header file to the code. • The functions (algorithms) must be the most efficient in terms of running time and memory usage. • Submit file strg.c. Complete the header with your student and contact information. Include sufficient comments in your code to facilitate code inspection. • Submit a report in PDF with following information: references (sources); error conditions and actions taken; brief algorithm; running time of the function (algorithm) and a brief explanation. See the template a2report.docx for an example. • See file a2output.txt for sample input and output. • Your code will be graded automatically by a script, so make sure to strictly follow the specifications. • Do not use any output statements (for example, printf) in your code, or they will mess up the grading scripts. • Use the main( ) function provided to test your code. Understanding the code in the main( ) function is part of the assignment. Do not change the code in the main( ) function in the final submission, or your program will mess up the grading script.
/***********************************
* Filename: strg.c 
* Author: Last name, first name
* Email: Your preferred email address
* Login ID: Your EECS login ID
************************************/
#include <stdio.h>
#define MAX_LENGTH 100   // DO NOT CHANGE THIS CONSTANT
/******************  YOUR CODE STARTS HERE ******************/
/************************************************************/
/*
 * Input: non-empty string s
 * Output: return the length of s
 */ 
int strgLen( char s[ ] )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}
/*
 * Input: non-empty string s
 * Output: copy the content of string s to string dest
 */ 
int strgCopy( char s[ ], char dest[ ] )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}
/*
 * Input: non-empty string s
 * Output: for each character in string s, if it is an alphabet, reverse the  
 * case of the character.  Keep the non-alphabet characters as is.  
 */ 
int strgChangeCase( char s[ ] )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}
/*
 * Input: non-empty strings s1 and s2
 * Output: Return the index where the first difference occurs.
 * Return -1 if the two strings are equal.
 */ 
int strgDiff( char s1[ ], char s2[ ] )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}
/*
 * Input: non-empty strings s1 and s2
 * Output: copy s1 and s2 to s3, interleaving the characters of s1 and s2.  
 * If one string is longer than the other, after interleaving, copy the rest 
 * of the longer string to s3.  
 */
int strgInterleave( char s1[ ], char s2[ ], char d[ ] )
{
   /* ADD YOUR CODE HERE */
  
   return 0;
}
/*******************  YOUR CODE ENDS HERE *******************/
/************************************************************/
/*********  DO NOT CHANGE ANYTHING BELOW THIS LINE IN THE FINAL SUBMISSION *********/
/* main() function 
 */
int main()
{
  char op[ MAX_LENGTH ]; 
  char str1[ MAX_LENGTH ]; 
  char str2[ MAX_LENGTH ];
  char str3[ MAX_LENGTH ];  
  int index;
  
  do {
    scanf( "%s %s", op, str1 );
    switch( op[ 0 ] )
    {
    case 'l':   // length
    case 'L':
      printf( "%d\n", strgLen( str1 ) );
      break;
          
    case 'c':   // copy
    case 'C':
      strgCopy( str1, str2 );
      printf( "%s\n", str2 );
      break;
      
     case 'h':   // cHange case
     case 'H':
      strgChangeCase( str1 );
      printf( "%s\n", str1 );
      break;     
                
    case 'd':  // difference  
    case 'D':
      scanf( "%s", str2 );
      index = strgDiff( str1, str2 );
      if ( index < 0 )
        printf( "Equal strings\n" );
      else
        printf( "%d\n", index );      
      break;    
    
    case 'i':  // interleave
    case 'I':
      scanf( "%s", str2 );    
      strgInterleave( str1, str2, str3 );
      printf( "%s\n", str3 );      
      break;
    case 'q':  // quit
    case 'Q':
      /* To quit, enter character (action) 'q' or 'Q' and an arbitrary string.
         This is not elegant but makes the code simpler.  */  
      /* Do nothing but exit the switch statement */    
      break;
            
    default:  
      printf( "Invalid operation %c\n", op[0] );         
    }  // end switch
  } while ( op[ 0 ] != 'q' && op[ 0 ] != 'Q' );
  
  return 0;
}
/***********************************
* Filename: strg.c 
* Author: Last name, first name
* Email: Your preferred email address
* Login ID: Your EECS login ID
************************************/
#include <stdio.h>
#define MAX_LENGTH 100 // DO NOT CHANGE THIS CONSTANT
/******************  YOUR CODE STARTS HERE ******************/
/************************************************************/
/*
 * Input: non-empty string s
 * Output: return the length of s
 */
int strgLen(char s[])
{
    /* ADD YOUR CODE HERE */
    char *sp = s; //declare a pointer to the fist character of the string
    // loop through the string till we find the string terminating character
    while (*sp != '\0')
        sp++;
    return sp - s;
}
/*
 * Input: non-empty string s
 * Output: copy the content of string s to string dest
 */
int strgCopy(char s[], char dest[])
{
    /* ADD YOUR CODE HERE */
    int i;
    /* loop through the string till we find the string terminator of s1*/
    for (i = 0; s[i] != '\0'; i++)
    {
        dest[i] = s[i]; //put character at ith index in s1 at ith index of dest
    }
    dest[i] = '\0'; // add string terminating character to properly end dest.
    return 0;
}
/*
 * Input: non-empty string s
 * Output: for each character in string s, if it is an alphabet, reverse the  
 * case of the character.  Keep the non-alphabet characters as is.  
 */
int strgChangeCase(char s[])
{
    /* ADD YOUR CODE HERE */
    int len = strgLen(s);         //find length of the string using our own method defined above
    for (int i = 0; i < len; i++) // loop through  the string till the end
    {
        if (s[i] >= 'a' && s[i] <= 'z') //if the character is a lowercase character subtract 32 from its ASCII value
        {
            s[i] = s[i] - 32;
        }
        else if (s[i] >= 'A' && s[i] <= 'Z') //if the character is a uppercase character add 32 to its ASCII value
        {
            s[i] = s[i] + 32;
        }
    }
    return 0;
}
/*
 * Input: non-empty strings s1 and s2
 * Output: Return the index where the first difference occurs.
 * Return -1 if the two strings are equal.
 */
int strgDiff(char s1[], char s2[])
{
    /* ADD YOUR CODE HERE */
    int len1 = strgLen(s1);
    int len2 = strgLen(s2);
    int length = len1 >= len2 ? len2 : len1; //determine the smaller string size
    int i;
    int diffFlag = 0;
    for (i = 0; i < length; i++)
    {
        if (s1[i] != s2[i]) //if the character at ith index do not match set diffFlag = 1
        {
            diffFlag = 1;
            break;
        }
    }
    if (diffFlag == 1)
        return i; //return the fist non-matching index
    else
        return -1;
}
/*
 * Input: non-empty strings s1 and s2
 * Output: copy s1 and s2 to s3, interleaving the characters of s1 and s2.  
 * If one string is longer than the other, after interleaving, copy the rest 
 * of the longer string to s3.  
 */
int strgInterleave(char s1[], char s2[], char d[])
{
    /* ADD YOUR CODE HERE */
    int len1 = strgLen(s1);
    int len2 = strgLen(s2);
    int length = len1 >= len2 ? len2 : len1;
    int i = 0, j = 0;
    for (i = 0, j = 0; i < length && j < length * 2; i++, j++) //loop through the strings s1 and s2
    {
        d[j] = s1[i]; //at even index add character from s1
        j++;
        d[j] = s2[i]; //at odd index add character from s2
    }
    if (length == len1) // if s1 is smaller, add remaining characters of s2 to the end of destination string d
    {
        while (j < (len1 + len2) && i < len2) 
        {
            d[j] = s2[i];
            j++;
            i++;
        }
    }
    else //if s1 is larger, add the characters of s1 to the end of destination string 
    {
        while (j < (len1 + len2) && i < len1)
        {
            d[j] = s1[i];
            j++;
            i++;
        }
    }
    d[j] = '\0'; //add the terminating character to destination string
    return 0;
}
/*******************  YOUR CODE ENDS HERE *******************/
/************************************************************/
/*********  DO NOT CHANGE ANYTHING BELOW THIS LINE IN THE FINAL SUBMISSION *********/
/* main() function 
 */
int main()
{
    char op[MAX_LENGTH];
    char str1[MAX_LENGTH];
    char str2[MAX_LENGTH];
    char str3[MAX_LENGTH];
    int index;
    do
    {
        scanf("%s %s", op, str1);
        switch (op[0])
        {
        case 'l': // length
        case 'L':
            printf("%d\n", strgLen(str1));
            break;
        case 'c': // copy
        case 'C':
            strgCopy(str1, str2);
            printf("%s\n", str2);
            break;
        case 'h': // cHange case
        case 'H':
            strgChangeCase(str1);
            printf("%s\n", str1);
            break;
        case 'd': // difference
        case 'D':
            scanf("%s", str2);
            index = strgDiff(str1, str2);
            if (index < 0)
                printf("Equal strings\n");
            else
                printf("%d\n", index);
            break;
        case 'i': // interleave
        case 'I':
            scanf("%s", str2);
            strgInterleave(str1, str2, str3);
            printf("%s\n", str3);
            break;
        case 'q': // quit
        case 'Q':
            /* To quit, enter character (action) 'q' or 'Q' and an arbitrary string.
         This is not elegant but makes the code simpler.  */
            /* Do nothing but exit the switch statement */
            break;
        default:
            printf("Invalid operation %c\n", op[0]);
        } // end switch
    } while (op[0] != 'q' && op[0] != 'Q');
    return 0;
}
OUTPUT: