Replace string and then place it again

1 Ansicht (letzte 30 Tage)
Mansour Al Asais
Mansour Al Asais am 9 Mär. 2020
Bearbeitet: BobH am 18 Mär. 2020
Hey, I want to make the output return # as many times as a certain string has, and then, change each # with the corresponding letter in the original string to get a cool ouput.
This is my code so far but I got stuck:
function a = jamstr(strd,time)
for i = 0:1:length(strd)
z = i + 1;
expression = '^.*';
replace = '#';
newStr = regexprep(strd(z),expression,replace)
fprintf(newStr);
pause(time);
a=z;
if z == length(strd)
break
end
end
end
  3 Kommentare
Mansour Al Asais
Mansour Al Asais am 18 Mär. 2020
jamstr('Matlabl rocks!',0.1)
What do you mean?
Ameer Hamza
Ameer Hamza am 18 Mär. 2020
Bearbeitet: Ameer Hamza am 18 Mär. 2020
Mansour, what is the expected output for
jamstr('Matlab rocks!',0.1)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

BobH
BobH am 18 Mär. 2020
Bearbeitet: BobH am 18 Mär. 2020
This is intentionally backwards from what you asked; adjusting it to start with all '#' is for you
function a = jamstr( strd, time )
fprintf( '%s', strd ); % print the entire string
% one backspace for each char
bkspaces = repmat('\b',size(strd));
fmt = [bkspaces '%s']; % format string for fprintf
for i = 1:length(strd)
newStr = strd;
newStr(i) = '#';
fprintf( fmt, newStr );
pause(time);
end
fprintf( fmt, strd ); % restore original
fprintf( '\n' ); % end on a fresh line
end

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by