How to compare strings within a loop

13 views (last 30 days)
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)
  1 Comment
Hassan Jawed
Hassan Jawed on 27 Apr 2020
Thank you for your kind co-operation, it works perfectly fine

Sign in to comment.

Accepted Answer

Sindar
Sindar on 27 Apr 2020
Edited: Sindar on 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 Comment
Sindar
Sindar on 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

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by