Posts

Find address of char, string, integer using c program

Find address of char, string, integer #include<stdio.h> #include<conio.h>  main()   {    char *chp,*sp;    int i;    char ch,s[10];    int *ip;    clrscr();    printf("Enter a char:");    scanf("%c",ch);    printf("Enter a string:");    scanf("%s",s);    printf("Enter a integer:");    scanf("%d",&i);    chp=&ch;    sp=s;    ip=&i;    printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");    printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);    printf("\nchar pointer value is:%u",chp);    printf("\nstring pointer value is:%u",sp);    printf("\nint pointer value is:%u",ip);    getch();   }

Subtraction of Two Matrices

Subtraction of Two Matrices C Program for subtraction of two matrices  #include<stdio.h> #include<conio.h> //Read Matrix void read_mat(float a[][10],int m,int n) { int i,j; printf("\n\nEnter %d X %d matrix below:\n",m,n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%f",&a[i][j]); } //Write Matrix void write_mat(float a[][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%10.2f",a[i][j]); printf("\n"); } } //Subtract matrices void sub_mat(float a[][10],float b[][10],float c[][10],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j] - b[i][j]; } //main function void main() { float x[10][10],y[10][10],z[10][10]; int m,n; clrscr(); //Accept two matrices printf("\n\nEnter order of matrix \n"); scanf("%d %d",&m,&n); read_mat(x,m,n); read_mat(y,m,n); //call sub_mat() function sub_mat(x,y,z,m,n);

Swapping of two Values using Functions

  c program to Swapping of two Values using Functions #include<stdio.h> #include<conio.h> int swapval ( int , int ); int swapref ( int *, int *); int a , b ; void main () { clrscr (); printf ( “enter the two values\n” ); scanf ( “ % d % d” ,& a ,& b ); printf ( “pass by value\n” ); printf ( “before function call a =% d b =% d “ , a , b ); swapval ( a , b ); printf ( “after function swapval a =% d b =% d “ , a , b ); printf ( “pass by reference\n” ); printf ( “before function call a =% d b =% d “ , a , b ); swapref (& a ,& b ); printf ( “after function swapref a =% d b =% d “ , a , b ); getch (); } swapval ( int x , int y ) { int t ; t = x ; x = y ; y = t ; printf ( “\nwith swap val x =% d y =% d” , x , y ); } swapref ( int * x , int * y ) { int * t ; * t =* x ; * x =* y ; * y =* t ; printf ( “\nwith swapref x =% d y =% d “ ,* x ,* y ); }

delete Characters from a given position in a given string

To delete n Characters from a given position in a given string /* Write a C program that uses functions to perform the following operations: To delete n Characters from a given position in a given string. */ #include <stdio.h> #include <conio.h> #include <string.h> void delchar(char *x,int a, int b); void main() { char string[10]; int n,pos,p; clrscr(); puts(“Enter the string”); gets(string); printf(“Enter the position from where to delete”); scanf(“%d”,&pos); printf(“Enter the number of characters to be deleted”); scanf(“%d”,&n); delchar(string, n,pos); getch(); } // Function to delete n characters void delchar(char *x,int a, int b) { if ((a+b-1) <= strlen(x)) { strcpy(&x[b-1],&x[a+b-1]); puts(x); } }

Total Number of Consonants in a String

Total Number of Consonants in a String #include<stdio.h> #include<conio.h> #include<string.h> void main() { int c=0,i,l,p; char a[10]; clrscr(); printf("program that gives total number of consonants in a string"); printf("\n\n\n\t\t------------INPUT-------------"); printf("\n\nenter any string");//taking input from the user scanf("%s",&a); l=strlen(a); for(i=0;i { if(a[i]=='a' || a[i]=='e' || a[i]=='o' || a[i]=='i' || a[i]=='u') c++; } p=l-c; printf("\n\n\n\t\t------------OUTPUT------------"); printf("\n\nthe total no. of consonants in the string are=%d",p);//printing output getch(); }

Tree sort- string array

Tree sort- string array #include "stdio.h" #include "string.h" #include "stdlib.h" struct tnode { char *str; struct tnode *left; struct tnode *right; }; void insert(struct tnode **p, char *value); void print(struct tnode *root); int main(void) { char line[1024]; struct tnode *root; root = NULL; while((fgets(line, 1024, stdin)) != NULL) insert(&root, line); print(root); return 0; } /* call by reference .. ! */ void insert(struct tnode **p, char *value) { if(!*p) { *p = (struct tnode *)malloc(sizeof(struct tnode)); (*p)->left = (*p)->right = NULL; (*p)->str = strdup(value); return; } if(strcmp(value, (*p)->str) < 0) insert(&(*p)->left, value); else insert(&(*p)->right, value); } /* inorder binary tree print ... */ void print(struct tnode *root) { if(root != NULL) { print(root->left); printf("%s", root->str); print(root->right); } }

Use of Strlen() Function

Use of Strlen() Function #include<stdio.h> #include<string.h> void main(void) { char str[31]; int len; printf("\nEnter any String"); gets(str); len=strlen(str); printf("\nNumber of Character in%s=%d\n",str,len); }