一旦我发现字符串s中存在的字符(字符串t中不存在),我需要立即断开两个循环 [英] I need to break out of two loops at once as soon as I find a character present in string s which is not present in string t
问题描述
<pre>#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int i,j,count=0;
char s[100],t[100];
scanf("%s", s);
scanf("%s", t);
int s1=strlen(s);
int t1=strlen(t);
if(strlen(s)!=strlen(t))
{
printf("-1");
}
else
{
for(i=0;i<strlen(s);i++)
{
for(j=0;j<strlen(t);j++)
{
if(s[i]==t[j])
{
if(i==j)
{
count=count+1;
}
}
}
}
if(count>0)
{
printf("%d",count);
}
else
{
printf("-1");
}
}
return 0;
}
我的尝试:
我尝试使用2个字符串搜索每个字符.....但是一旦我得到两个不相关的字符,我需要打破2个嵌套循环并打印-1
What I have tried:
I tried searching for each character using 2 strings.....but as soon as i get two uneqUAL CHARACTERS i need to break out of the 2 nested loops and print -1
推荐答案
简单的方法:重构你的代码,使循环在一个单独的函数中。
然后你可以使用return
退出整个函数和两个循环。
Easy way: refactor your code so the loops are in a separate function.
You can then usereturn
to exit the whole function and both loops.
我需要打破两个一旦我发现字符串s中存在一个字符就不会出现在字符串t中
I need to break out of two loops at once as soon as I find a character present in string s which is not present in string t
你错了。
你知道这是一个字符串仅当字符串 t $ c中的比较时,字符串
s
不在字符串 t
中$ c>已完成。
除此之外,你知道内部循环结束后是否字符串不在字符串中。
注意:你的代码很奇怪,它是n它正在做什么。
事实上,很难知道代码在做什么。
You are wrong.
You know that a char of string s
is not in string t
only when comparison in string t
is done.
Said otherwise, you know if a char is not in string t only after inner loop is ended.
Note: your code is weird and it is not what it is doing.
In fact, it is difficult to know what the code is doing.
这篇关于一旦我发现字符串s中存在的字符(字符串t中不存在),我需要立即断开两个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!