Question

In: Computer Science

Please read the Specifications carefully. Also do not use any C library. Program Specifications Assuming that...

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;
}

Solutions

Expert Solution

/***********************************
* 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:



Related Solutions

Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
Please read the following program requirements carefully:  You are working as an eBay Seller, where...
Please read the following program requirements carefully:  You are working as an eBay Seller, where your customers are only in New York (n), New Jersey (j) and Pennsylvania (p).  Your C++ program is supposed ask the name of the state where the purchase is made. (n or j or p)  Next your program asks the amount of purchase.  Then, you calculate the Total amount by adding the proper State sales tax calculation to the Purchase Amount...
C++ Please read the question carefully and make sure that the function prototypes given are used...
C++ Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part. The output example is provided at the end of the question. First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array. Part 1: Write a function...
C++ Please read the question carefully and match the output example given at the end! Question...
C++ Please read the question carefully and match the output example given at the end! Question In this project you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (​<<​), addition (​+​), and subtraction (​-​). insertion (​<<​): The insertion operator is used to send the values of a vector to an ostream object, if a vector has two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT