Question

Problem 1 In this problem, you will write two functions. The first function takes in a...

Problem 1

In this problem, you will write two functions. The first function takes in a string and returns that string without any dashes. The second function takes in the first and last name and returns a string that is lastname_firstname and uses the previous function to remove any dashes (-) in the name.

Note that you’ll be testing it by calling it from the command line; you’ll call it from a script in problem 3.

Deliverables:

  1. Function file that takes in a string and returns a string without a dash

    1. A script is not necessary for this problem

  2. Call that function from the command line and show that it works

    1. strNoDash = RemoveDash(‘first-name);

      1. For this problem, you will not need to create a script to show your function works.

  3. Function file that takes in first and last name and returns the joined name

    1. This should include the RemoveDash function ran for both the first and last name

  4. Call the function from the command line and show that it works

    1. str = JoinName(‘first’, ‘last’)

    2. str = JoinName(‘first, ‘last-name);

    3. str = JoinName(‘first-name, ‘last-name);

Step by Step Instructions:

Create a function file that takes in a string and returns the same string with no dashes.

  • Add code to your function to check for the dash (-) in either the first or last name

    • Try strfind(‘first’). What do you get?

    • try strfind(‘first-dash’). What do you get?

    • You’ll need isempty to check for an empty return

  • If you have a dash, you need to delete it. There’s two ways to do this. Either set that element to the empty array

    • array(k) = [] - deletes the kth element from array

    • array = strcat( array(1:k-1), array(k+1:end) ) – set the array to the first and second halves

Create another function file. It should take in two variables and return one.

  • The inputs should be the first and last names as strings

  • Start with using strcat or sprintf to join the names together. Check that it works with ‘first’ and ‘last’ (call from the command line)

    • Note: One trick is to copy the first line of the function file, delete the function key word, and then put your own variable values in. This makes sure that you have the inputs and the outputs in the right order

    • I.e., copy function function [strOut] = JoinNames( strFirst, strLast ) to the command line,

    • And then edit to be

      • strOut = JoinNames(‘first’, ‘last’)

  • Make sure to implement the RemoveDash function for both names in the JoinNames function

    • Call this on both inputs from within your join string function


Self-check:

>> strOut = RemoveDash('first-dash')

strOut =

firstdash

>> str = JoinName('first', 'last')

str =

last_first

>> str = JoinName('first-dash', 'last')

str =

last_firstdash

>> str = JoinName('first-dash', 'last-dash')

str =

lastdash_firstdash

>>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

*** Here is your solution. Please provide proper indentation as shown in the script ***

#######################################################################################

def RemoveDash(s):
new_s=""
for ch in s:
if ch!='-':
new_s+=ch
return new_s

def JoinName(fname,lname):
n_fname=RemoveDash(fname)
n_lname=RemoveDash(lname)
name=n_lname+"_"+n_fname
return name

##############################################################################

CMD screenshot:

*** If you have any doubt regarding the solution, please do ask in the comments ***

Add a comment
Know the answer?
Add Answer to:
Problem 1 In this problem, you will write two functions. The first function takes in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Write a Python program to create userids: You work for a small company that keeps the...

    Write a Python program to create userids: You work for a small company that keeps the following information about its clients: • first name • last name • a user code assigned by your company. The information is stored in a file clients.txt with the information for each client on one line (last name first), with commas between the parts. In the clients.txt file is: Jones, Sally,00345 Lin,Nenya,00548 Fule,A,00000 Your job is to create a program assign usernames for a...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • In C++ First create the two text file given below. Then complete the main that is given. There ar...

    In C++ First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12...

  • PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number)...

    PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number) and prints: My name is <name> and I am <age> years old(where name and age are the inputs) 2. Write a function that takes as input a and b and returns the result of: ??ab 3. Write a function that takes as input a and b and returns the result of: ??ab if it is valid to divide a and b 4. Write a...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • 1. Write a function that takes as input a directory. The function will search through that...

    1. Write a function that takes as input a directory. The function will search through that directory and its full subdirectory tree, building a count of the files by their extensions. As in the lecture, use a dictionary to keep track of the counts. The return-value of this function will be a dictionary of (key,value) pairs where the key will be the file extension and the value will be the number of files with that extension. For example, for the...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT