Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Sunday, 6 October 2013

custom strcpy function in c to copy a string

#include<stdio.h>
#include<conio.h>
void mystrcpy(char *dest,const char*src)
{
while(*src)
{
 *dest=*src;
 src++;
 dest++;
}
*dest='\0';
}
int main()
{
  char str1[10]="Welcome";
  char str2[10];
  clrscr();
  printf("\nEnter a string: ");
  gets(str2);
  puts(str1);
  puts(str2);
  //stcpy(str1,str2); -- way to call library function
  mystrcpy(str1,str2);
  puts(str1);
  puts(str2);
  return 0;
}

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

                  
    
    

C program to swap two integer numbers without using third variable

#include<stdio.h>
#include<conio.h>
int main()
{
    float a,b;
    printf("enter the values to swap \n");
    scanf("%f%f",&a,&b);
    printf("before swapping a:%f b:%f\n",a,b);
    /*a=a+b;
    b=a-b;
    a=a-b;*/
    a=a*b;
    b=a/b;
    a=a/b;
    printf("After swapping a:%f b:%f\n",a,b);
    _getch();
    return 0;
}

Tuesday, 21 August 2012

Does C support function overloading like C++?

Look at the printf() function in C, that may lead one to think that C supports function overloading. Because, in C you can have printf("%d", DecimalValue) and printf("%f", FloatValue). This looks a lot like function overloading, because we are using the same function name to handle different arguments differently.

Actually, this is not a case of function overloading – the printf function is just using a feature of C known as variable argument lists. This should not be confused with function overloading. So, to answer the question, Standard C does not support function overloading.


As an interesting side note, C++ doesn’t really have function overloading. What it does have is a means of faking it: the C++ compiler actually ‘mangles’ (or changes) function names according to the function’s parameters. So, functions that share the same name but have different numbers or types of parameters can be differentiated when invoked. Also, since the ‘mangling’ of function names is not standardized, it’s usually difficult to link object files compiled by different C++ compilers.



Thursday, 9 August 2012

implementation of single linked list in c language


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}NODE;
NODE *first=NULL,*last,*temp,*newnode;
void creation()
{
int ch;
do
{
newnode=(NODE*)malloc(sizeof(NODE));
newnode->link=NULL;
printf("\n enter the data of the node : ");
scanf("%d",&newnode->data);
if(first==NULL)
{
first=newnode;
}
else
{
last->link=newnode;
}
last=newnode;
printf("\n do u want to add one more node [1/0] : ");
scanf("%d",&ch);
}
while(ch==1);
}
void insertion()
{
int ch,data;
printf("\n 1.at first \n 2.at middle \n 3.at end ");
printf("\n enter your choice :");
scanf("%d",&ch);
newnode=(NODE *)malloc(sizeof(NODE));
newnode->link=NULL;
printf("\n enter the data for the new node :");
scanf("%d",&newnode->data);
switch(ch)
{
case 1:
newnode->link=first;
first=newnode;
break;
case 2:
printf("\n enter the data of the node after which you want to insert :");
scanf("%d",&data);
temp=first;
while(temp->data==data)
temp=temp->link;
newnode->link=temp->link;
temp->link=newnode;
break;
case 3:
temp=first;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
last=newnode;
break;
default:
printf("\n invalid  choice ");
free(newnode);
}
}
void deletion()
{
int ch,data;
NODE *temp1;
if(first==NULL)
printf("\n list is empty ");
else
{
printf("\n1.first node \n2.middle node \n3.ending node ");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
temp=first;
printf("\n deleted element is : %d ",first->data);
first=first->link;
free(temp);
break;
case 2:
printf("\n enter the data of the node that you want to delete : ");
scanf("%d",&data);
temp=first;
while(temp->link->data!=data)
temp=temp->link;
temp1=temp->link;
temp->link=temp->link->link;
free(temp1);
break;
case 3:
temp=first;
while(temp->link->link!=NULL)
temp=temp->link;
printf("\n deleted element is : %d ",temp->link->data);
temp1=temp->link;
temp->link=NULL;
free(temp1);
break;
default:
printf("\ninvalid choice");
}
}
}
void length()
{
int l=0;
temp=first;
while(temp!=NULL)
{
temp=temp->link;
l++;
}
printf("\n length of the list is : %d",l);
}
void display()
{
temp=first;
if(temp==NULL)
printf("\n list is empty...nothing to display ");
else
{
printf("\n");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n 1.creation \n 2.insertion \n 3.deletion \n 4.display \n 5.length");
printf("\n 6.exit \n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
creation();
break;
case 2:
insertion();
break;
case 3:
deletion();
break;
case 4:
display();
break;
case 5:
length();
break;
case 6:
exit(0);
default:
printf("\ninvalid choice\n");
}
}
}

implementation of circular queue using arrays in c language

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 5
int queue[MAX],front=0,rear=0;
void insertion()
{
if(front==(rear+1)%MAX)
printf("\n queue is full..cannot insert ");
else
{
rear=(rear+1)%MAX;
printf("\n Enter the element to insert in the queue : ");
scanf("%d",&queue[rear]);
}
}
void deletion()
{
if(front==0&&rear==0)
printf("\n queue is empty...nothing to delete ");
else
{
printf("\n deleted element is : %d ",queue[front]);
front=(front+1)%MAX;
}
if(front==rear)
{
front=0;
rear=0;
}
}
void display()
{
int i;
if(front==0&&rear==0)
printf("\n queue is empty...nothing to display ");
else
{
if(front==0)
front++;
for(i=front;i!=rear;i=(i+1)%MAX)
{
printf("%d ",queue[i]);
}
printf("%d ",queue[i]);
}
}
void length()
{
int length=0,i;
if(front==0&&rear==0)
length=0;
else
{
for(i=front;i!=rear;i=(i+1)%MAX)
length++;
}
printf("\n length of the queue is : %d",length);
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n 1.insertion\n 2.deletion\n 3.display\n 4.length\n 5.exit");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
length();
break;
case 5:
exit(0);
default:
printf("\n invalid choice...try again");
}
}
}

implementation of stack using arrays in c language

#include<stdio.h>
#include<conio.h>
#define max 5
int stack[max],ele,top;
void push();
void pop();
void display();
void topelement();
int main()
{
    char ch;
    top=-1;
    printf("1.push\n2.pop\n3.display\n4.topelement\n5.end\nenter your choice:");
    scanf("%d",&ele);
    do
    {
                    switch(ele)
                    {
                              case 1:
                                   push();
                                   break;
                              case 2:
                                   pop();
                                   break;
                              case 3:
                                   display();
                                   break;
                              case 4:
                                   topelement();
                                   break;
                              case 5:
                                   exit(0);
                                   break;
                    }
             printf("\ndo u want to perform more stack operations[y/n]:");
             ch=_getch();
             if(ch=='y')
             {
                        printf("\n1.push\n2.pop\n3.display\n4.topelement\n5.end\nenter your choice:");
                        scanf("%d",&ele);
             }
    }
    while(ch=='y');
    _getch();
    return 0;
}
void push()
{
     if(top==max-1)
     {
                   printf("stack is full cannot insert anymore\n");
                   return;
     }
     else
     {
                 printf("enter the element do u want to insert:");
                 scanf("%d",&ele);
                 top++;
                 stack[top]=ele;
     }
}
void pop()
{
     if(top==-1)
     {
                printf("stack is empty..no elements to delete\n");
                return;
     }
     else
     {
                top--;
                printf("deleted element from the stack is : %d",stack[top+1]);
     }
}
void display()
{
     int i;
     if(top==-1)
     {
                printf("stack is empty..no elements to display\n");
                return;
     }
     else
     {
         printf("the elememts of the stack are : ");
         for(i=0;i<=top;i++)
                            printf("%d\t",stack[i]);
     }
}
void topelement()
{
     if(top==-1)
     {
                printf("stack is empty\n");
                return;
     }
     else
                printf("the top most element of the stack is : %d",stack[top]);
}

implementation of linearsearch using arrays in c language

#include<stdio.h>
#include<conio.h>
int main()
{
    int key,i,a[]={11,3,56,78,93,45,26,28,6,71};
    printf("enter a values to search:");
    scanf("%d",&key);
    for(i=0;i<=9;i++)
    {
                     if(a[i]==key)
                     break;
    }
    if(i==10)
    printf("search element is not found");
    else
    printf("search element is found at %d location",i+1);
    getch();
    return 0;
}

binary search implementation in c language using arrays

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,key,a[]={1,5,9,11,23,43,56,59,78,99},l=0,h=9,mid;
    printf("enter a values to search:");
    scanf("%d",&key);
    while(l<=h)
    {
               mid=(l+h)/2;
               if(a[mid]==key)
               {
                              printf("search element is found at %d location",mid);
                              getch();
                              //exit(0);
                              return;
               }
               a[mid]>key?(h=mid-1):(l=h+1);
    }
    printf("search element is not found");
   getch();
   return 0;
}

implementation of insertion sorting through single linked list in c


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*new,*temp;
void creation()
{
int data;
do
{
printf("enter the data of node which you want to sort :");
scanf("%d",&data);
new=(struct node*)malloc(sizeof(struct node));
new->data=data;
new->link=NULL;
if(first==NULL || new->data < first->data)
{
new->link=first;
first=new;
}
else
{
temp=first;
while(temp->link!=NULL&&new->data>=temp->link->data)
temp=temp->link;
new->link=temp->link;
temp->link=new;
}
printf("\n do u want to add one more node[1/0] : ");
scanf("%d",&data);
}while(data);
}

void display()
{
if(first==NULL)
{
printf("\n list is empty... nothing to display");
}
else
{
temp=first;
printf("\n sorted list is :\n");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
}
}
int main()
{
creation();
display();
getch();
return 0;
}

Thursday, 2 August 2012

Is there something we can do in C but not in C++?

The answer is Yes and its seems to be funny but the interviewer may trouble you on this..

Declare variable names which are keywords in C++ but not C.


#include < stdio.h >
int main(void)
{
int  new=3,delete=4;
return 0;
}


this above code will compile in c compiler but not in c++ compiler because new,delete are keywords specific to c++ and they are not in c.

How to swap the two nibbles in a byte ?

unsigned char swapNibbles(unsigned char c)
{
unsigned char temp1, temp2;
temp1 = c & 0x0F;
temp2 = c & 0xF0;
temp1=temp1 << 4;
temp2=temp2 >> 4; 

return(temp2|temp1); //adding the bits
}

int main(void)
{
char ch=0x34;
printf("\nThe exchanged value is %x",swapNibbles(ch));
return 0;
}

How to fast multiply a number by 7?

(num << 3)- num

This is same as

num*8 - num = num * (8-1) = num * 7

How can we sum the digits of a given number in single statement?

void main()
{
int num=123456; 
int sum=0;

for(;num > 0;sum+=num%10,num/=10); // This is the "single line".

printf("\nsum = [%d]\n", sum);
}


Given two strings A and B, how would you find out whether the characters in B were a subset of the characters in A?

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

int isSubset(char *a, char *b);

int main()
{
char str1[]="defabc";
char str2[]="abcfed";

if(isSubset(str1, str2)==0)
{
printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
}
else
{
printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
}

getch();
return(0);
}


// Function to check if characters in "b" are a subset
// of the characters in "a"

int isSubset(char *a, char *b)
{
int letterPresent[256];
int i;

for(i=0; i < 256; i++)
letterPresent[i]=0;

for(i=0; a[i]!='\0'; i++)
letterPresent[a[i]]++;

for(i=0; b[i]!='\0'; i++)
if(!letterPresent[b[i]])
return(1);

return(0);
}

Write your own C program to implement the atoi() function

int myatoi(const char *string);

int main(int argc, char* argv[])
{
printf("\n%d\n", myatoi("1992")); 
getch();
return 0;
}

int myatoi(const char *string)
{
int i;
i=0;
while(*string)
{

i=i*10;
i=i + (*string - '0');
string++;


// Dont increment i!
}
return(i);
}