How to compare strings within a loop
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hassan Jawed
am 26 Apr. 2020
Kommentiert: Hassan Jawed
am 27 Apr. 2020
I am trying to create a program in which I want to compare my saved data which I have stored in cell with respect to the input given by the user. Since I have lot of data I dont want to compare one with each other manually, but I want to use loops command. Please point out my mistakes in below
clear all; close all; clc;
str{1} = 'http://vacationet.com/resort.php?id=22';
str{2} = 'http://jokusoftware.cz/file.php?id=icqj3';
str{3} = 'http://www.nichegardens.com/catalog/item.php?id=19114';
a0=input('Paste the http link to check its vulunerability ', 's');
n=1
for i=str{n};
if (strcmpi(a0,i))
sprintf('%s is vulunerable',a0);
else
n=n+1;
continue;
end
end
sprintf('%s is non vulunerable',a0)
Akzeptierte Antwort
Sindar
am 27 Apr. 2020
Bearbeitet: Sindar
am 27 Apr. 2020
No need to loop, strcmpi will compare a string to all strings in an array. If you don't care which string matches, use "any":
str{1} = 'url1';
str{2} = 'url2';
str{3} = 'url3';
a0=input('Paste the http link to check its vulunerability ', 's');
if any(strcmpi(a0,str))
sprintf('%s is vulunerable',a0);
else
sprintf('%s is non vulunerable',a0)
end
(Answers doesn't like links, thinks they mean spam)
1 Kommentar
Sindar
am 27 Apr. 2020
If you were to do it in a loop, make sure you're looping over the strings, not the characters in them (as you appear to be):
str{1} = 'url1';
str{2} = 'url2';
str{3} = 'url3';
a0=input('Paste the http link to check its vulunerability ', 's');
isvulnerable = false;
for ind=1:length(str)
if strcmpi(a0,str{ind})
sprintf('%s is vulunerable',a0);
% if you find a match, end the loop and record
isvulnerable=true;
break
end
end
% after checking all of them, print if no matches were found
if ~isvulnerable
sprintf('%s is non vulunerable',a0)
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!