You are given a file consisting of students’ names in the following form: lastNa
ID: 3627615 • Letter: Y
Question
You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes as input a string, consists of a student’s name, and returns the string consisting of the altered name. Use the string function find to find the index of , ; the function length to find the length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName.What would the algorithm be to this problem.
Explanation / Answer
please rate - thanks everyone does algorithms a little differently hope ths is good open file loop: more names in file? yes: read name into string variable name call function sending it variable name go to loop no: exit program function set start=0 set end to length of variable name using function length end=name.length() use function find to find the location of , put this in loc loc=name.find(',',start) using substr set last = name.substr(start,loc) add 2 to loc to place it at the first letter of the first name use function find to get the location of the space put it in loc2 loc2=name.find(' ',loc) using substr set first=name.substr(loc,loc2-loc) loc2-loc=length of first name using substr set middle=name.substr(loc2+1,end-loc2) loc2+1 is after the space output first space middle space last space return
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.