检查两个字符串是否相等 [英] Check if two strings are equal

查看:98
本文介绍了检查两个字符串是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们必须检查的问题,如果两个srings是否相等

例如:

Here is the problem we have to check that if two srings are equal or not
for example:

s1[]=akash and s2[]=ashka 

相等

我的程序显示

are equal
my program is showing

NO

总是为每个字符串;

这里t是数字测试用例;



我尝试过:



always for every string ;
Here t is the number of test cases;

What I have tried:

#include<stdio.h>
#include<string.h>
int main(){
int t,i,j;
scanf("%d",&t);
while(t>0){
    char s1[100],s2[100];
    scanf("%s ",s1);
    scanf("%s",s2);
    int count=0;
    int found[100];
    for(i=0;i<strlen(s1)-1;i++){
        for(j=0;j<strlen(s1);j++){
            if(s1[i]==s2[j]){
                found[i]=1;
                break;
            }


        }
    }
       for(i=0;i<strlen(s1);i++){
           if(found[i]!=1){
          count=1;
          break;

           }
       }
    if(count==1)
    printf("NO");
    else
    printf("YES");







    t--;
}



}

推荐答案

在生产代码中,我会使用 strcmp ,或者更好的是, strncmp 函数(注意你已经在使用 C 字符串库 strlen 功能)。



一个简单的 strcmp 实现可以是

Well in production code, I would use the strcmp, or, better, the strncmp function (note you are already using the C string library strlen function).

A simple strcmp implementation could be
int strcmp(const char * s1, const char * s2)
{
  for (;;)
  {
    if ( *s1 > *s2) 
      return 1;
    else if ( *s1 < *s2)
      return -1;
    else if (*s1 == 0) 
      return 0;
    ++s1, ++s2;
  } 
}


我不太确定相等的定义,但错误是你的循环有不同的停止条件:

I'm not quite sure about the definition of "equal" here but the error is that your loops have different stop conditions:
for(i=0;i<strlen(s1)-1;i++){
/* ... */
}
for(i=0;i<strlen(s1);i++){

第一个循环少一次迭代,以便第二个循环处理最后一个项目(发现[strlen(s1)-1] )从未设置。



附注:

使用正常(非调试)版本的C / C ++不初始化本地变量。所以你应该通过代码来做到这一点:

The first loop has one iteration less so that the last item processed by the second loop (found[strlen(s1)-1]) is never set.

Side note:
Local variables are not initialised with C/C++ with normal (non-debug) builds. So you should do that by code:

int found[100];
memset(found, 0, sizeof(found));

当所有未设置的项目包含值1时,不这样做可能会导致错误的TRUE输出。

Not doing so might result in a wrong "TRUE" output when all unset items contain the value "1".


这是明白错误的

This ,is plain wrong
for(i=0;i<strlen(s1)-1;i++){
    for(j=0;j<strlen(s1);j++){
        if(s1[i]==s2[j]){
            found[i]=1;
            break;
        }
    }
}



字符串 s1 [] =akash s2 [] =ashka等于

s1 [0] == s2 [0]和s1 [1] == s2 [1]和s1 [ 2] == s2 [2]和s1 [3] == s2 [3] ...

使用您的代码, s1 [] =abcd和< b> s2 [] =dcba将匹配。



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的cpde应该做什么,它不是找到错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


The strings s1[]="akash" and s2[]="ashka" are equal if
s1[0]==s2[0] and s1[1]==s2[1] and s1[2]==s2[2] and s1[3]==s2[3] ...
With your code, s1[]="abcd" and s2[]="dcba" will match.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于检查两个字符串是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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