How to compare strings within a loop

조회 수: 4 (최근 30일)
Hassan Jawed
Hassan Jawed 2020년 4월 26일
댓글: Hassan Jawed 2020년 4월 27일
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
Hassan Jawed
Hassan Jawed 2020년 4월 27일
Thank you for your kind co-operation, it works perfectly fine

댓글을 달려면 로그인하십시오.

채택된 답변

Sindar
Sindar 2020년 4월 27일
편집: Sindar 2020년 4월 27일
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
Sindar
Sindar 2020년 4월 27일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by