ALLInterview.com :: Home Page KalAajKal.com
 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                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
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 ?    15 Yes 11 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 ?    21 Yes 13 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 ?    8 Yes 7 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 ?    8 Yes 1 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 ?    14 Yes 7 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 ?    9 Yes 3 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 ?    8 Yes 0 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 ?    4 Yes 0 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 ?    3 Yes 1 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 ?    1 Yes 1 No
Kedir
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 11
void main()
{
char a[10],b[10];
int i=0;
scanf("%s%s",a,b);
if(strlen(a)!=strlen(b))
printf("they are different strings");
else
{
while(a[i]!='\0')
{
if(a[i]==b[i])
i++;
else 
{
printf("They are different strings");
exit(0);
}
printf("Both are same");
}
getch();
}
 
Is This Answer Correct ?    4 Yes 1 No
Belsia
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 12
void main()
{
char a[10],b[10];
int i=0;
scanf("%s%s",a,b);
if(strlen(a)!=strlen(b))
printf("they are different strings");
else
{
while(a[i]!='\0')
{
if(a[i]==b[i])
i++;
else 
{
printf("They are different strings");
exit(0);
}
}
printf("Both are same");
}
getch();
}
 
Is This Answer Correct ?    1 Yes 1 No
Belsia
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 13
I have been seeing lot of answers posted on top of mine.
here is another highly optimized version.

int str_cmp (const char *s1, const char *s2)
{
   while (*s1 == *s2++)
     if (*s1++ == 0)
        return (0);
   return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}
before marking it as not an answer, I urge you to try it once!
Trust me, it works.
 
Is This Answer Correct ?    2 Yes 2 No
Sujith
 
  Re: Write a program to compare two strings without using the strcmp() function
Answer
# 14
Guys What the hell????

strcmp tells you whether strings are equal or not, if not which is greater then other.....

So first compare their length...

example let 
str1="abc";
str2="abcde";

now compare their lengths, if len1 >len2 means str1 is greater else
if len2>len1 then str2  is greater

else the above compare is required...........
 
Is This Answer Correct ?    0 Yes 0 No
Akash Aggarwal
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
let's take a code struct FAQ { int a; char b; float c; double d; int a[10]; }*temp; now explain me how the memory will be allocated for the structure FAQ and what address will be in the structure pointer (temp)....................  7
Program to find the value of e raised to power x using while loop N-Tech3
WHAT IS THE DIFFERENCE BETWEEN malloc() and calloc() in c file management?  6
#include<stdio.h> main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } what will happen if you executed this code? Ramco4
main() { int i = 1; int num[] = {1,2,3,4}; num[i] = i++; printf("%d", num[i]); } what will be the output? } NDS15
#define d 10+10 main() { printf("%d",d*d); }  3
#include<stdio.h> int fun(); int i; int main() { while(i) { fun(); main(); } printf("hello \n"); return 0; } int fun() { printf("hi"); } answer is hello.how??wat is tat while(i) mean? Intel4
a memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Etrance" l=strlen(s); what is the value of l ? a.20 b.8 c.9 d.21 TCS4
i=20,k=0; for(j=1;j<i;j=1+4*(i/j)) { k+=j<10?4:3; } printf("%d", k); HCL6
write a string copy function routine?  1
how we can make 3d venturing graphics on outer interface Microsoft1
1 232 34543 4567654 can anyone tell me how to slove this c question  3
what is the advantage of using SEMAPHORES to ORDINARY VARIABLES??? NSN1
7. Identify the correct argument for the function call fflush() in ANSI C: A)stdout B)stdin C)stderr D)All the above Accenture8
How would you print out the data in a binary tree, level by level, starting at the top? Microsoft4
write a program for size of a data type without using sizeof() operator?  7
How can we see the Expanded source code and compiled code for our source program in C?  1
what is the use of a array in c  4
What are data breakpoints? Adobe1
If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5). Microsoft4
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com