ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Programming Languages >> C
 
 


 

 
 C interview questions  C Interview Questions (2248)
 C++ interview questions  C++ Interview Questions (1106)
 VC++ interview questions  VC++ Interview Questions (342)
 Delphi interview questions  Delphi Interview Questions (510)
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions (644)
Question
Write a program to compare two strings without using the 
strcmp() function
 Question Submitted By :: Rajesh
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 1
#include<stdio.h>
int str_cmp(const char *s1, const char *s2)
{
unsigned int i = 0, diff;
while(*(s1+i) && *(s2+i))
{
diff = (*(s1+i)-*(s2+i));
if(!diff)i++;
else break;
}
return diff;
}
int main()
{
printf("chuma %d ", str_cmp("abcd","abcde"));
return 0;
}
U can use this as a prototype and enhance this. I havent
even tried compilng this.
Sujith
 
Is This Answer Correct ?    112 Yes 107 No
Sujith
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 2
#include<stdio.h>
main()
{

//please read two strings int str1 and str2//

while(str1[i]!='/0' &&str2[i]!='/0')
if(str1[i]!=str2[i])
flag=1;
if(flag==1)
printf("equal");
}
 
Is This Answer Correct ?    126 Yes 214 No
Sriramaraju
 
 
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 3
int str_cmp( const char *str1 , const char *str2)
{


while(*str1 != '\0')
{
if( *str1 == *str2)
{
str1++;
str2++;
}
else
break;
}


return *str1 - *str2;
}
 
Is This Answer Correct ?    64 Yes 75 No
Shaiju . A
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 4
// Optimize the above soln...
#include<stdio.h>
int str_cmp(const char *s1, const char *s2)
{
unsigned int i = 0, diff;
while(s1[i]!= '\0' || s2[i] != '\0'){
diff = s1[i] - s2[i];
if(!diff)i++;
else break;
}
return diff;
}
int main(int argc, char *argv[1])
{
printf("chuma %d ", str_cmp("abcd","abcde"));
return 0;
}
 
Is This Answer Correct ?    35 Yes 47 No
Sandeep A
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 5
#include <stdio.h>
#include <string.h>

void stringcmp(char s1[], char s2[]);

int main()
{

char str1[10],str2[10];

printf("\nEnter first String:");
scanf("%s",str1);

printf("\nEnter second String:");
scanf("%s",str2);

stringcmp(str1,str2);

return 0;
}

void stringcmp(char *s1, char *s2)
{
int i,j;

for(i=0;s1[i]!='\0';i++)
{
for(j=0;s2[j]!='\0';j++)
{
if(s1[i] == s2[j])
continue;
}
}

if (i==j)
{
printf("String s1:%s and s2:%s are EQUAL\n",s1,s2);
}
else
printf("String s1:%s and s2:%s are NOT EQUAL\n",s1,s2);

}
 
Is This Answer Correct ?    83 Yes 88 No
Ria Varughese
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 6
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()

{
clrscr();
int c=0;
char a[10];

char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;

}

}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";

getch();
}
 
Is This Answer Correct ?    87 Yes 41 No
Waqar Nawaz
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 7
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()

{
clrscr();
int c=0;
char a[10];

char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;

}

}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";

getch();
}
 
Is This Answer Correct ?    47 Yes 28 No
Vijay.benzamin
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 8
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()

{
clrscr();
int c=0;
char a[10];

char b[10];
gets(a);
gets(b);
for(int i=0,j=0;b[i]!='\0'||a[j]!='\0';i++,j++)
{
if(a[i]!=b[j])
{
c++;

}

}
if(c==0)
cout<<"string match";
else
cout<<"string does not match";

getch();
}
 
Is This Answer Correct ?    57 Yes 22 No
Premkumar
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 9
1 yes
2 yes
 
Is This Answer Correct ?    28 Yes 33 No
Kedir
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 10
=> ALL ARE CORRECT ANSWER 
Is This Answer Correct ?    26 Yes 47 No
Kedir
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
what is differnence b/w macro & functions   1
Binary tree traversing Qualcomm 1
void main() {int i=2; printf("%d%d%d",i,++i,i++); getch(); }   8
how to convert binary to decimal and decimal to binary in C lanaguage BPO 7
Write a program to generate prime factors of a given integer? Microsoft 8
code snippet for creating a pyramids triangle ex 1 2 2 3 3 3   4
Why c++ is called c++ and not c+? EBS 8
c pgm count no of lines , blanks, tabs in a para(File concept)   2
What is Generic pointer? What is the purpose of Generic pointer? Where it is used?   3
Struct(s) { int a; long b; } Union (u) {int a; long b; } Print sizeof(s)and sizeof(u) if sizeof(int)=4 and sizeof(long)=4 Mascot 2
#include<stdio.h> int main() { int i=2; int j=++i + ++i + i++; printf("%d\n",i); printf("%d\n",j); } Infosys 14
WHAT IS THE DIFFERANCE BITWIN GETS();AND SCANF(); TCS 2
 
For more C Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com