How to compare strings within a loop

17 visualizaciones (últimos 30 días)
Hassan Jawed
Hassan Jawed el 26 de Abr. de 2020
Comentada: Hassan Jawed el 27 de Abr. de 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)
  1 comentario
Hassan Jawed
Hassan Jawed el 27 de Abr. de 2020
Thank you for your kind co-operation, it works perfectly fine

Iniciar sesión para comentar.

Respuesta aceptada

Sindar
Sindar el 27 de Abr. de 2020
Editada: Sindar el 27 de Abr. de 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 comentario
Sindar
Sindar el 27 de Abr. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by