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 will happen when freeing memory twice   2
What is period operator in c? Wipro 2
Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not require more than the basic operators (if, for, math operators, etc.). • Assumptions • Don’t worry about overflow or underflow • Stop at the 1st invalid character and return the number you have converted till then, if the 1st character is invalid return 0 • Don’t worry about exponential (e.g. 1e10), instead you should treat ‘e’ as an invalid character • Write it like real code, e.g. do error checking • Go though the string only once • Examples • “1.23” should return 1.23 • “1a” should return 1 • “a”should return 0 Qualcomm 6
to find the program of matrix multiplication using arrays Bhel 6
main() { int i=400,j=300; printf("%d..%d"); } ME 13
What are .h files and what should I put in them?   3
What is the output of the following progarm? #include<stdio.h> main( ) { int x,y=10; x=4; y=fact(x); printf(“%d\n”,y); } unsigned int fact(int x) { return(x*fact(x-1)); } A. 24 B. 10 C. 4 D. none   2
SIR PLS TELL ME THE CODE IN C LANGUAGE TO PRINT THE FOLLOWING SERIES 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1   4
WHAT IS FLOAT?   3
what is use of loop? Infosys 10
2. What does static variable mean?   2
write a program to insert an element at the specified position in the given array in c language IBM 5
 
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