Size of array in C

#include <stdio.h> #define N 5 /* Size of array */ main( ) { int A[N]; /* Space for A[0]………A[4] is allocated */ int j,sum=0; for(i=0; i<N; i++) A[i]=i*i; /* Initialize the array */ for(i=0;i<N; i++) /* Display array elements */ printf(“ A[%d]=%d”, i, A [i] ); for(i =0; i <N; ++i) /* Find total */ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Access individual elements of array by pointers

Access individual elements of array by pointers In the below program, an integer array num is declared and initialized. ptr is an integer pointer. Pointer ptr points to the num by the statement ptr=num;. We can access every element of an array with the pointer. void main() { int i,num[5]={10,20,30,40,50}; int * ptr; clrscr(); ptr=num; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Re-arranging strings using a two dimensional array in C

The program takes a list of strings and re-arranging them into either alphabetical or reverse alphabetical order.   #include <stdio.h> #include <string.h>   void reorder(int n,char x[10][6]) { char temp[10][6]; int i,item;   for(item=0;item<n-1;++item) for(i=item+1;i<n;++i) if(strcmp(&x[item][0],&x[i][0])>0) { strcpy(&temp[0][0],&x[item][0]); strcpy(&x[item][0],&x[i][0]); strcpy(&x[i][0],&temp[0][0]); } }   void reverse(int n,char x[10][6]) { char temp[10][6]; int i,item;   for(item=0;item<n-1;++item) for(i=item+1;i<n;++i) […]
Continue reading…

Enjoyed this post? Share it!

 
 

strcpy and strncpy in C

#include <stdio.h> #include <string.h>   int main(){ char s1[80],s2[10]; int n;   printf("Enter two strings : "); scanf("%s%s",s1,s2);   strcpy(s1,s2); printf("nString created by strcpy is %s.",s1); printf("nLength of the new string is %d.",strlen(s1));   strncpy(s1,s2,5); s1[5]=”; printf("nString created by strncpy using 5 characters is %s.n",s1); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Example program using strcat and strncat in C

#include <stdio.h> #include <string.h>   int main() { char s1[80],s2[10]; int n;   printf("Enter two strings : "); scanf("%s%s",s1,s2);   strcat(s1,s2); printf("nString built by strcat is %s.",s1); printf("nLength of the string built by strcat is %d.",strlen(s1));   strncat(s1,s2,5); printf("nString built by strncat using 5 characters is %s.n",s1); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Using strncmp in C language

The function strncmp has three arguments. The first two arguments are two strings. The third is an integer that specifies the number of characters to compare. #include <stdio.h> #include <string.h>   int main() { char s1[80],s2[80]; int outcome;   printf("Enter two strings : "); scanf("%s%s",s1,s2); outcome=strncmp(s1,s2,2); printf("nValue returned by strncmp is %d.n",outcome); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrate the use of the strcmp function in C

The strcmp function takes two string arguments. Let us call these arguments s1 and s2, respectively. An integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2. #include <stdio.h> #include <string.h>   int main() { char s1[80],s2[80]; int […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program to reverse an input string and print the ASCII sum of the characters comprising the string

#include <stdio.h>   int main() { const int MaxLine=100; // Sets the limit of characters in string char Line[MaxLine]; int i,Sum=0,ch;   /*Read in the string*/ printf("nEnter a set of characters : n"); for(i=0;((ch=getchar())!=’n’);++i) Line[i]=(char)ch; Line[i]=”;   /*Print the reverse of the line*/ printf("nThe reverse of the line entered is…n"); while(i>0) putchar(Line[–i]);   /*Print the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program to print the elements of a 2-D array in Row major and Column major orders

  #include <stdio.h>   int main() { int Temp[3][2]; int i, j;   /*Read in the values for the array Temp*/ printf("nEnter the elements of array Temp(3×2) row-wise…n"); for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&Temp[i][j]);   /*Print the array Temp*/ printf("nThe array Temp as entered is…n"); for(i=0;i<3;i++) { for(j=0;j<2;j++) printf("t%d",Temp[i][j]); printf("n"); }   /*Print the array Temp in row-major […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program to add two matrices using functions in C

#include <stdio.h>   int main() { const int M=3,N=3; // M holds no of rows, N holds no of cols int c[M][N],d[M][N],e[M][N]; int i, j;   /*Read in the values for the c and d matrices*/ printf("nEnter the elements of matrices c and d row-wise"); printf("nIn the format c[i][j],d[i][j]…n"); for(i=0;i<M;i++) for(j=0;j<N;j++) scanf("%d,%d",&c[i][j],&d[i][j]);   /*Print the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program to add two matrices in C

#include <stdio.h>   int main() { const int M=3,N=3; // M holds no of rows, N holds no of cols int c[M][N],d[M][N],e[M][N]; int i, j;   /*Read in the values for the c and d matrices*/ printf("nEnter the elements of matrices c and d row-wise"); printf("nIn the format c[i][j],d[i][j]…n"); for(i=0;i<M;i++) for(j=0;j<N;j++) scanf("%d,%d",&c[i][j],&d[i][j]);   /*Print the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrate the use of 1-D arrays in C

Fill an array, print out values and sum the elements of the array. #include <stdio.h>   int main() { int a[6]; // Space for a[0],a[1], … , a[5] is allocated int i,Sum=0;   /*Fill the array*/ for(i=0;i<=5;i++) a[i]=i*i; /*Print the values in the array*/ printf("n"); for(i=0;i<=5;i++) printf("a[%d]=%d.n",i,a[i]); /*Calculate the sum of the values of the […]
Continue reading…

Enjoyed this post? Share it!