Question

In: Computer Science

Write a function bracket_by_len that takes a word as an input argument and returns the word...

Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space available.

Program: C

Solutions

Expert Solution

Input:

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

char *bracket_by_len(char*); // funtion declaration
int main() {
    char string[20]; // expecting the string to be of 20letters
    scanf("%[^\n]%*c", string); // taking string as input until enter is pressed
    char a[20]; // to store the result string
    strcpy(a,bracket_by_len(string)); // copying the returned string in a[]
    printf("\n String is %s",a); //printing the new string with brackets
    return 0;
}
char *bracket_by_len(char* string){  // funtion definition
  int size = strlen(string) + 5; //calculating the size to allocate, here +5 is 4 for opening and closing brackets and 1 for buffer
  char *str = malloc(size); 
    if(strlen(string)<5){ // checking for string length
        
        strcpy (str, "<<"); // << will be copied to str
        strcat (str, string); // string will be concatenated with str becoming <<String
        strcat (str, ">>"); // >> will be concatenated with str becoming <<String>>

         return str; // returning <<String>>
    }
    else if(strlen(string) >=5 && strlen(string) <=10){ // checking for string 5 to 10 letters long 
        strcpy (str, "(*"); // (* will be copied to str
        strcat (str, string); // string will be concatenated with str becoming (*String
        strcat (str, "*)"); // *) will be concatenated with str becoming (*String*)

         return str; // returning (*String*)
    }
    else{ // This else will execute only if length is more than 10
        strcpy (str, "/+"); // /+ will be copied to str
        strcat (str, string); // string will be concatenated with str becoming /+String
        strcat (str, "+/");  // +/ will be concatenated with str becoming /+String+/
         return str; // returning /+String+/
    }
}

Output:


Related Solutions

Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT