一旦我发现字符串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

查看:85
本文介绍了一旦我发现字符串s中存在的字符(字符串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 use return to exit the whole function and both loops.


Quote:

我需要打破两个一旦我发现字符串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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆