C Programming Tutorial

Advertisements

Write a program to compare two strings are same or not in C



       #include<stdio.h>
       #include<conio.h>

       void main()
       {

              char str1[50],str2[50];
              char found='y';
              int i;
              clrscr();

              printf("\nEnter 1st string : ");
              gets(str1);
              
              printf("\nEnter 2nd string : ");
              gets(str2);

              for(i=0;str1[i];i++)
              {
                     if(str1[i] != str2[i])
                     {
                           found='n';
                           break;
                     }              
              }
              
              if(found=='y')       
              printf("\nBoth strings are same.");
              else
              printf("\nBoth strings are not same.");

              getch();    
    }

   Output : 

              Enter 1st string : Kumar Wadhwa
              Enter 2nd string : Kumar Wadhwa
              Both strings are same.




Advertisement