Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
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--;
}



}
Posted
Updated 28-Aug-18 2:56am
Comments
Richard MacCutchan 28-Aug-18 8:10am    
Use 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
C
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;
  } 
}
 
Share this answer
 
Comments
Richard MacCutchan 28-Aug-18 9:31am    
else if (*s1 == 0)
But what if s2 still has more characters?
CPallini 28-Aug-18 11:16am    
But what if s2 still has more characters?
Then you are possibly using a quantum computer. :-D
Richard MacCutchan 28-Aug-18 11:35am    
Er?
CPallini 28-Aug-18 11:46am    
if (*s1 is not greater than *s2) and (*s1 is not less than *s2) then (*s1 is equal to *s2). Don't you agree with me?
Richard MacCutchan 28-Aug-18 13:58pm    
s1 = "abd";
s2 = "abdx";
If you stop comparing at the end of s1 then they are not equal.
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++){
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:
C++
int found[100];
memset(found, 0, sizeof(found));
Not doing so might result in a wrong "TRUE" output when all unset items contain the value "1".
 
Share this answer
 
This ,is plain wrong
C++
for(i=0;i<strlen(s1)-1;i++){
    for(j=0;j<strlen(s1);j++){
        if(s1[i]==s2[j]){
            found[i]=1;
            break;
        }
    }
}

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900