Monday, 31 October 2011

Program to delete duplicate elements in an array


#include<stdio.h>
int main(){
  int arr[50];
  int *p;
  int i,j,k,size,n;
  printf("\nEnter size of the array: ");
  scanf("%d",&n);
  printf("\nEnter %d elements into the array: ",n);
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
  size=n;
  p=arr;
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
         if(i==j){
             continue;
         }
         else if(*(p+i)==*(p+j)){
             k=j;
             size--;
             while(k < size){
                 *(p+k)=*(p+k+1);
                 k++;
              }
              j=0;
          }
      }
  }
  printf("\nThe array after removing duplicates is: ");
  for(i=0;i < size;i++){
    printf(" %d",arr[i]);
  }
  return 0;
}

Program to concatenate two strings without using string functions


#include<stdio.h>
#include<conio.h>
void main()
{
    char a[50],b[50];
    int i,j,n,k;
    clrscr();
    printf("\n Please Give The STRING OF A : ");
    scanf("%s",a);
    printf("\n Please Give The STRING OF B : ");
    scanf("%s",b);
    for(i=0,k=0;a[i]!='\0';i++)
        k++;
    for(j=0,n=0;b[j]!='\0';j++)
        n++;
    printf("VALUE OF K=%d",k);
    printf("VALUE OF N=%d",n);
    for(i=k,j=0;i<=n+k;i++,j++)
            a[i]=b[j];


    printf("\n THE Concatenated string is %s .",a);
    getch();
}

Program to sort an array using bubble sort


#include<stdio.h>
main()
{
        int n,i,j,temp;
        printf("Enter the number of elements: ");
        scanf("%d",&n);
        int a[n];
        printf("Enter the elements in the array\n");
        for(i=0;i<n;i++)
                scanf("%d",&a[i]);
        for(i=0;i<n;i++)
        {
                for(j=i+1;j<n;j++)
                {
                        if(a[i]>a[j])
                        {
                                temp=a[i];
                                a[i]=a[j];
                                a[j]=temp;
                        }
                }
        }
        printf("\nThe sorted array is - \n" );
        for(i=0;i<n;i++)
                printf("%d, ",a[i]);
}

Program to count number of sentences words and letters in a paragraph


#include<stdio.h>
main()
{
        char ch[150];
        int i,c=0,c1=0,c2=0;
        printf("Enter a Paragraph \n");
        gets(ch);
        for(i=0;ch[i]!='\0';i++)
        {
                if(ch[i]=='.'||ch[i]=='!'||ch[i]=='?')
                {       c++;
                        c1++;
                }
                else if(ch[i]==' '||ch[i]==',')
                        c1++;
                else if(ch[i]>65&&ch[i]<123)
                        c2++;
        }
        printf("\nNumber of letters = %d \nNumber of words=%d ",c2,c1);
        printf("\nNumber of Sentences = %d\n",c);
}

Program to search an element using linear search or binary search (menu driven program)


#include<stdio.h>
void main()
{
int a[25],beg,item,last,n,num,i,ch,mid,f=0;
printf("menu\n");
printf("\n 1.linear search");
printf("\n 2.binary search");
printf("\n enter the choice");
scanf("%d",&ch);
if(ch==1)
{
printf("\n enter the number of elements in the array");
scanf("%d",&n);
printf("\n enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the item to be searched");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\n item found at position%d",i+1);
break;
}
}
if(i==n)
printf("\n item not found");
}
if(ch==2)
{
printf("\nenter the number of elements in the array");
scanf("%d",&n);
printf("enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("item to be searched");
scanf("%d",&item);
last=n-1;
mid=(beg+last)/2;
while(beg<=last)
{
if(item==a[mid])
{
printf("\n item found at position %d",mid+1);
break;
}
else if(a[mid]>item)
last=mid-1;
else beg=mid+1;
mid=(beg+last)/2;
}
}
}

Program to convert a string to upper case


#include<stdio.h>
#include<string.h>
main()
{
        char s[50];
        int i,l;
        printf("Enter some text: ");
        gets(s);
        l=strlen(s);
        for(i=0;i<l;i++)
        {
                if(s[i]>='a'&&s[i]<='z')
                        s[i]-=32;
        }
        printf("Text after converting to upper case: ");
        puts(s);
}

Program to copy a file to another file


#include<stdio.h>
void main()
{
        FILE *fp1,*fp2;
        char ch;
        char fname1[30],fname2[30];
        printf("Enter the source file: ");
        fflush(stdin);
        scanf("%s",fname1);
        printf("Enter the destination file: ");
        fflush(stdin);
        scanf("%s",fname2);
        fp1=fopen(fname1,"r");
        fp2=fopen(fname2,"w");
        if(fp1==NULL)
        {
                printf("\n cannot open file %s for reading",fname1);
                exit(1);
        }
        else if(fp2==NULL)
        {
                printf("\n cannot open file %s for writing",fname2);
                exit(1);
        }
        else
        {
                ch=getc(fp1);
        while(ch!=EOF)
        {
                putc(ch,fp2);
                ch=getc(fp1);
        }
        fclose(fp1);
        fclose(fp2);
        printf("\n files copied");
        }
}

Program to count vowels consonents and spaces in a string




#include<stdio.h>
main()
{
int i,cons=0,vow=0,sp=0;
char a[100];
printf("Enter the sentence\n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='A')
vow=vow+1;
else if(a[i]=='e'||a[i]=='E')
vow=vow+1;
else if(a[i]=='i'||a[i]=='I')
vow=vow+1;
else if(a[i]=='o'||a[i]=='O')
vow=vow+1;
else if(a[i]=='u'||a[i]=='U')
vow=vow+1;
else if(a[i]==' ')
sp=sp+1;
else
cons=cons+1;
}
printf("Total number of vowels entered = %d\n",vow);
printf("Total number of consonents entered = %d\n",cons);
printf("Total number of spaces entered = %d\n",sp);
}

Program to find smallest in an array using pointer


#include<stdio.h>


main()
{
        int i,n;
        printf("\nHow many elements?: ");
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
        {       printf("Element %d: ",i+1);
                scanf("%d",&a[i]);
        }
        printf("\nCreating pointer now...");
        int *p,small;
        p = a;
        small=*p;
        i=1;
        while(i<n)
        {
                if(small > *p)
                        small=*p;
                i++;
                p++;
        }
        printf("\nSmallest number: %d \n",small);
}

Program to merge and sort two arrays


#include<stdio.h>
void sort(int a[],int);
void main()
{
        int a[20],c[10],i,m,n,j;
        printf("Enter the no.of elements in 1st array- \n");
        scanf("%d",&m);
        printf("Enter the 1st array - \n");
        for(i=0;i<m;i++)
                scanf("%d",&a[i]);
        printf("Enter the no.of elements in the 2nd array");
        scanf("%d",&n);
        printf("Enter the 2nd array");
        for(i=0;i<n;i++)
                scanf("%d",&c[i]);
        sort(a,m);
        printf("1st sorted array - \n");
        for(i=0;i<m;i++)
                printf("%d, ",a[i]);
        sort(c,n);
        printf("2nd sorted array - \n");
        for(i=0;i<n;i++)
        printf("%d, ",c[i]);
        j=m;
        for(i=0;i<n;i++)
        {a[j]=c[i];
        j++;}
        sort(a,m+n);
        printf("Merged and Sorted array - \n");
        for(i=0;i<m+n;i++)
                printf("%d, ",a[i]);
}
void sort(int a[],int n)
{
        int i,j,temp;
        for(i=0;i<n;i++)
        {       for(j=i+1;j<n;j++)
                        if(a[i]>a[j])
                        {       temp=a[i];
                                a[i]=a[j];
                                a[j]=temp;
                        }
        }
}