Sunday, 6 October 2013

custom strcmp function in c to compare two strings

#include<stdio.h>
#include<string.h>
#include<conio.h>
int mystrcmp(const char*s1,const char*s2)
{
int r=0;
while(*s1!='\0'||*s2!='\0')
{
 if(*s1!=*s2)
 {
 r=(int)(*s1-*s2);
 return r;
 }
 s1++;
 s2++;
}
return r;
}

int main()
{
  char s1[10]="Hello";
  char s2[10]="HeLlo";
  int d;
  clrscr();
  puts(s1);
  puts(s2);
  //d=strcmp(s1,s2);
  d=mystrcmp(s1,s2);
  printf("\nDiff is:%d",d);
  getch();
  return 0;
}

C program to find length of the string using custom strlen function


#include<stdio.h>
#include<conio.h>
int main()
{
  char src[10];
  int l;
  int mystrlen(const char*);
  clrscr();
  printf("\nEnter a string: ");
  gets(src);
  //l=strlen(src);
  l=mystrlen(src);
  printf("\nString lenght:%d",l);
  getch();
  return 0;
}
int mystrlen(const char*src)
{
int c=0;
while(*src!='\0')
{
++c;
src++;
}
return c;
}

C program to print fibonacci series upto a given number

#include<stdio.h>
#include<conio.h>
int main()
{
    int m=0,n=1,temp,a;
    printf("enter the value upto which you want to print fibonacci numbers\n");
    scanf("%d",&a);
    printf("%d\n%d\n",m,n);
    while(n<a)
    {
               temp=m;
               m=n;
               n=temp+n;
               while(n<a)
               {
               printf("%d\n",n);
               break;
               }
    }
    _getch();
    return 0;
}
              
    
    
    

C program to find gcd of N inputs

#include<stdio.h>
#include<conio.h>
#define size 100 
int Gcd(int *);
int n=0;
int main()
{
    int x[size],z=0;
    printf("\n for how many values do u want to find gcd????::::");
    scanf("%d",&n);
    printf("\n enter %d values to find gcd ...",n);
    while(z<n)
    {
            scanf("%d",&x[z]);
            z++;
    }
    x[n]=Gcd(x);
    if(x[n]==-1)
    {
                printf("\n gcd doesn't exist....");
    }
    else if(x[n]==-2)
    {
                
    }
    else
    {
                printf("gcd of given numbers is : %d",x[n]);
    }
    _getch();
    return 0;
}
int Gcd(int *ptr)
{
   int a[size],c,d,i=0,j=1,k=0,z=0;
   while(k<n)
   {
                   a[k]=*ptr;
                   ptr++;
                   k++;
   }
    while(z<n)
    {
              if(a[z]==0)
              {
                   printf("\n gcd of the given numbers is : 0");
                   goto a;
              }
              z++;
    }          
   while(i<n)
   {
           while(j<n)
           {
              if(a[i]>a[j])
              {
                           c=a[i];
                           a[i]=a[j];
                           a[j]=c;
              }
              j++;
           }
           i++;
   }
   c=a[0];
   d=n-1;     
   while(c>1)
   {
             while(a[d]%c==0)
             {
                             if(d>1)
                             {
                                    d--;
                                    continue;
                             }
                             else 
                             return c;
             }
             c--;
             if(c==1)
             return -1;
   }
   a:
         return -2;
}

C program to reverse given number

//reversing a number 
#include<stdio.h>
#include<conio.h>
int main()
{
    int sum=0,n;
    printf("enter a number to reverse : ");
    scanf("%d",&n);
    while(n)
    {
            sum=sum*10+n%10;
            n=n/10;
    }
    printf("%d",sum);
_getch();
return 0;
}


i/p : 1234
o/p : 4321