フィルターのクリア

Plz help me with this function related question

11 ビュー (過去 30 日間)
Malik Shahid
Malik Shahid 2017 年 12 月 7 日
コメント済み: KL 2017 年 12 月 7 日
Hi I am trying to answer a question. But for some reason I am unable to execute the code in MATLAB. Could anyone just check and see where the problem is. The question is
  1. Write a getString() function that prompts the user for a string and returns it.
  2. Write a getChar() function that prompts the user for a character and returns it.
  3. Write a findandReplace() function that accepts a string, an old character and a new character then returns the string after replacing all the occurrences of the old character with the new. (Do NOT use loops)
  4. Write a main script to do the following:
  • Call getString() function to get the string from the user.
  • Call getChar() function to get the old character from the user.
  • Call getChar() function to get the new character from the user.
  • Call findandReplace() to replace all occurrence of the old character with the new character in the string.
  • Print the returned string.
Here is the answer so far. Please help me locate the error and correct the code.
function s=getString()
s=input('Enter A String: ','s');
disp(s);
end
function c=getChar()
c=input('Enter A Character: ','s');
disp(c);
end
function fandr = findandReplace(str,old,new)
fandr = strrep(str,old,new);
end
s=getString;
c=getChar;
d=getChar;
fanfr=findandReplace(str,old,new);
disp(fandr);

回答 (2 件)

KL
KL 2017 年 12 月 7 日
when you call findandReplace function in your main script, you should pass the inputs you received from the user. You have stored them in different names in your scripts (not str, old and new)
fanfr=findandReplace(?,?,?);
  2 件のコメント
Malik Shahid
Malik Shahid 2017 年 12 月 7 日
I get the following error
Undefined function or variable 'getString'.
Error in Q2d (line 1) s=getString;
Could you please write the correct code to call these functions?
KL
KL 2017 年 12 月 7 日
The better way is to save your functions in separate files.
%filename: getString.m
function s=getString()
s=input('Enter A String: ','s');
disp(s);
end
and also similarly for the other two functions. Then use your calls to the function from another script,
%somename.m
s=getString;
c=getChar;
d=getChar;
fandr=findandReplace(?,?,?);
disp(fandr);

サインインしてコメントする。


Rafael Hernandez-Walls
Rafael Hernandez-Walls 2017 年 12 月 7 日
change
disp(fandr)
for
disp(fanfr);

カテゴリ

Help Center および File ExchangeMatrix Computations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by