You are given a file consisting of students’ names in the following form: lastNa
ID: 3627621 • 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
Input the string into str Declare newStr Use find to, well, find the index of ',' in the string, store index as ind newStr=str.substr(ind+1,str.length()-1) newStr=newStr+" "+str.substr(0,ind) find ',' in newStr, store as ind if ind is -1 (meaning ',' is not in newStr), end the program break else newStr=newStr.substr(0,ind)+" "+newStr.substr(ind+1,newStr.length()-1) newStr will now contain "firstName middleName lastName" and it accounts for when there is no middle name =]
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.