Showing posts with label count. Show all posts
Showing posts with label count. Show all posts

Saturday, August 9, 2014

Program to count number of occurrences of a word in a sentence.

Simple C program to count the number of occurrences of a word in a sentence. If you need help, feel free to ask.


#include<stdio.h>
#include<string.h>
main()
{
  int len,c=0,i=0,j=0,ans=0;
  char str1[50],str2[20],new[20];
  printf("\n enter the text: ");
  gets(str1);
  printf("\n enter the word: ");
  gets(str2);
  len=strlen(str1);
  for(i=0;i<=len;i++)
   {
     if(str1[i]!=' ')
      {
       new[j]=str1[i];
       j++;
      }
     if(str1[i]==' '||str1[i]=='\0')
       {
           new[j]='\0';
           j=0;
           ans=strcmp(new,str2);
           if(ans==0)
             c++;
        }
    }
   printf("\n the number of occurence is %d\n",c);
}

Addition, multiplication and transpose of matrices

This is a program for the addition, multiplication and transpose of matrices. If you have any doubts, please let me know.

#include<stdio.h> main() { int m,n,i,j,k,p,q,r,a[25][25],b[25][25],c[25][25],ch; printf("\n Enter the rows and columns"); scanf("%d %d",&m,&n); printf("\n Enter the value of the array"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("\n MENU \n 1)addition 2)multiplication 3)transpose \n Enter your choice"); scanf("%d",&ch); if(ch==1) { printf("\n Enter the value of the array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; } } printf("\n the array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==2) { printf("\n Enter the value of array b"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][i]; } } } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",c[i][j]); } printf("\n"); } } if(ch==3) { for(i=0;i<m;i++) { for(j=0;j<n;j++) b[i][j]=a[j][i]; } printf("\n the new array is \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d ",b[i][j]); } printf("\n"); } } }

Program to check for a leap year.

Simple C program to check whether a year is leap year or not. If you have any doubts, please let me know.

#include<stdio.h> void main() { int num; printf ("enter number"); scanf ("%d",&num); if(num%4==0&&num%100!=0)||(num%400==0) { printf ("leap year"); } else { printf ("no"); } }

Program to print out largest of 3 numbers

Program to print out the largest of 3 numbers. If you have any doubts, please let me know.


#include<stdio.h> void main() { int large,x,y,z; printf("enter the 3 numbers"); scanf("%d",&x); scanf("%d",&y); scanf("%d",&z); large=(x>y?(x>z?x:z):(y>z?y:z)); printf("%d",large); }

Program for counting years, weeks and days.

Simple C program for counting the number of years, weeks and days. If you have any doubts, let me know


#include<stdio.h> main() { int c,year,x,week,day; printf("enter no of days"); scanf("%d",&c); year=c/365; x=c%365; week=x/7; day=x%7; printf("YEAR %d",year); printf("WEEK %d",week); printf("DAY %d",day); }