ARRAY
Array: An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. Properties of Array:  The array contains the following properties.  Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes.  Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.  Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
Declaration of C Array We can declare an array in the c language in the following way. data_type array_name[array_size]; Now, let us see the example to declare the array. int marks[5]; Here, int is the data_type, marks are the array_name, and 5 is the array_size
Initialization of C Array  The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example. 1. marks[0]=80;//initialization of array 2. marks[1]=60; 3. marks[2]=70; 4. marks[3]=85; 5. marks[4]=75;
C array example #include<stdio.h> int main(){ int i=0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; //traversal of array for(i=0;i<5;i++){ printf("%d n",marks[i]); }//end of for loop return 0; } Output 80 60 70 85 75
Length of an array: #include <unistd.h> int main() { int array[] = {15, 50, 34, 20, 10, 79, 100}; int n; n = sizeof(array); printf("Size of the given array is %dn", n/sizeof(int)); return 0; }
Two Dimensional Array in C  The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.
Declaration of two dimensional Array in C: The syntax to declare the 2D array is given below.  data_type array_name[rows][columns];  Consider the following example.  int twodimen[4][3];  Here, 4 is the number of rows, and 3 is the number of columns. Initialization of 2D Array in C: int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Two-dimensional array example in C #include<stdio.h> int main(){ int i=0,j=0; int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //traversing 2D array for(i=0;i<4;i++){ for(j=0;j<3;j++){ printf("arr[%d] [%d] = %d n",i,j,arr[i][j]); }//end of j }//end of i return 0; } Output arr[0][0] = 1 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 2 arr[1][1] = 3 arr[1][2] = 4 arr[2][0] = 3 arr[2][1] = 4 arr[2][2] = 5 arr[3][0] = 4 arr[3][1] = 5 arr[3][2] = 6
Storing elements in a matrix and printing it. 1. #include <stdio.h> 2. void main () 3. { 4. int arr[3][3],i,j; 5. for (i=0;i<3;i++) 6. { 7. for (j=0;j<3;j++) 8. { 9. printf("Enter a[%d][%d]: ",i,j); 10. scanf("%d",&arr[i][j]); 11. } 12. } 13. printf("n printing the elements ....n"); 14. for(i=0;i<3;i++) 15. { 16. printf("n"); 17. for (j=0;j<3;j++) 18. { 19. printf("%dt",arr[i][j]); 20. } 21. } 22. } Output Enter a[0][0]: 56 Enter a[0][1]: 10 Enter a[0][2]: 30 Enter a[1][0]: 34 Enter a[1][1]: 21 Enter a[1][2]: 34 Enter a[2][0]: 45 Enter a[2][1]: 56 Enter a[2][2]: 78 printing the elements .... 56 10 30 34 21 34 45 56 78
Passing Array to Function in C Consider the following syntax to pass an array to the function.  functionname(arrayname);//passing array Methods to declare a function that receives an array as an argument There are 3 ways to declare the function which is intended to receive an array as an argument. 1.First way:  return_type function(type arrayname[])  Declaring blank subscript notation [] is the widely used technique. 2.Second way:  return_type function(type arrayname[SIZE])  Optionally, we can define size in subscript notation []. 3.Third way:  return_type function(type *arrayname)
C language passing an array to function example 1. #include<stdio.h> 2. int minarray(int arr[],int size){ 3. int min=arr[0]; 4. int i=0; 5. for(i=1;i<size;i++){ 6. if(min>arr[i]){ 7. min=arr[i]; 8. } 9. }//end of for 10. return min; 11. }//end of function 12. 13. int main(){ 14. int i=0,min=0; 15. int numbers[]={4,5,7,3,8,9};//declaration of array 16. 17. min=minarray(numbers,6);//passing array with size 18. printf("minimum number is %d n",min); 19. return 0; 20. } Output minimum number is 3
C function to sort the array 1. #include<stdio.h> 2. void Bubble_Sort(int[]); 3. void main () 4. { 5. int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; 6. Bubble_Sort(arr); 7. } 8. void Bubble_Sort(int a[]) //array a[] points to arr. 9. { 10. int i, j,temp; 11. for(i = 0; i<10; i++) 12. { 13. for(j = i+1; j<10; j++) 14. { 15. if(a[j] < a[i]) 16. { 17. temp = a[i]; 18. a[i] = a[j]; 19. a[j] = temp; 20. } 21. } 22. } 23. printf("Printing Sorted Element List ...n"); 24. for(i = 0; i<10; i++) 25. { 26. printf("%dn",a[i]); 27. } 28. } Output Printing Sorted Element List ... 7 9 10 12 23 23 34 44 78 101
STRINGS
C Strings  The string can be defined as the one-dimensional array of characters terminated by a null ('0').  The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0.  The termination character ('0') is important in a string since it is the only way to identify where the string ends.  When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
There are two ways to declare a string in c language. 1.By char array 2.By string literal Let's see the example of declaring string by char array in C language. 1. char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0' }; As we know, array index starts from 0, so it will be represented as in the figure given below.
 While declaring string, size is not mandatory. So we can write the above code as given below:  char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0'};  We can also define the string by the string literal in C language. For example:  char ch[]="javatpoint";  In such case, '0' will be appended at the end of the string by the compiler. Difference between char array and string literal: There are two main differences between char array literal and. We need to add the null character '0' at the end of the array by ourself whereas, it is appended internally by the compiler in the case of the character array. The string literal cannot be reassigned to another set
Example program: #include<stdio.h> #include <string.h> int main(){ char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0'}; char ch2[11]="javatpoint"; printf("Char Array Value is: %sn", ch); printf("String Literal Value is: %sn", ch2); return 0; } Output: Char Array Value is: javatpoint String Literal Value is: javatpoint
Traversing String: Hence, there are two ways to traverse a string. 1. By using the length of string 2. By using the null character. Using the length of string: Let's see an example of counting the number of vowels in a string. #include<stdio.h> void main () { char s[11] = "javatpoint"; int i = 0; int count = 0; while(i<11) { if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count ++; } i++; } printf("The number of vowels %d",count); } Output The number of vowels 4
Using the null character: Let's see the same example of counting the number of vowels by using the null character. #include<stdio.h> void main () { char s[11] = "javatpoint"; int i = 0; int count = 0; while(s[i] != NULL) { if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count ++; } i++; } printf("The number of vowels %d",count); } Output: The number of vowels 4
C gets() and puts() functions:  The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings. C gets() function:  The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user. Declaration  char[] gets(char[]); Reading string using gets(): #include<stdio.h> void main () { char s[30]; printf("Enter the string? "); gets(s); printf("You entered %s",s); } Output: Enter the string? javatpoint is the best You entered javatpoint is the best
C puts() function:  The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1. Declaration:  int puts(char[]) Let's see an example to read a string using gets() and print it on the console using puts(). #include<stdio.h> #include <string.h> int main(){ char name[50]; printf("Enter your name: "); gets(name); //reads string from user printf("Your name is: "); puts(name); //displays string return 0; } Output: Enter your name: Sonoo Jaiswal
C String Functions:  There are many important string functions defined in "string.h" library. No. Function Description 1) strlen(string_name) returns the length of string name. 2) strcpy(destination, source) copies the contents of source string to destination string. 3) strcat(first_string, second_string) concats or joins first string with second string. The result of the string is stored in first string. 4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0. 5) strrev(string) returns reverse string. 6) strlwr(string) returns string characters in lowercase. 7) strupr(string) returns string characters in uppercase.

C Programming Unit-3

  • 1.
  • 2.
    Array: An array isdefined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. Properties of Array:  The array contains the following properties.  Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes.  Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.  Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
  • 3.
    Declaration of CArray We can declare an array in the c language in the following way. data_type array_name[array_size]; Now, let us see the example to declare the array. int marks[5]; Here, int is the data_type, marks are the array_name, and 5 is the array_size
  • 4.
    Initialization of CArray  The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example. 1. marks[0]=80;//initialization of array 2. marks[1]=60; 3. marks[2]=70; 4. marks[3]=85; 5. marks[4]=75;
  • 5.
    C array example #include<stdio.h> intmain(){ int i=0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; //traversal of array for(i=0;i<5;i++){ printf("%d n",marks[i]); }//end of for loop return 0; } Output 80 60 70 85 75
  • 6.
    Length of anarray: #include <unistd.h> int main() { int array[] = {15, 50, 34, 20, 10, 79, 100}; int n; n = sizeof(array); printf("Size of the given array is %dn", n/sizeof(int)); return 0; }
  • 7.
    Two Dimensional Arrayin C  The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.
  • 8.
    Declaration of twodimensional Array in C: The syntax to declare the 2D array is given below.  data_type array_name[rows][columns];  Consider the following example.  int twodimen[4][3];  Here, 4 is the number of rows, and 3 is the number of columns. Initialization of 2D Array in C: int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
  • 9.
    Two-dimensional array examplein C #include<stdio.h> int main(){ int i=0,j=0; int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //traversing 2D array for(i=0;i<4;i++){ for(j=0;j<3;j++){ printf("arr[%d] [%d] = %d n",i,j,arr[i][j]); }//end of j }//end of i return 0; } Output arr[0][0] = 1 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 2 arr[1][1] = 3 arr[1][2] = 4 arr[2][0] = 3 arr[2][1] = 4 arr[2][2] = 5 arr[3][0] = 4 arr[3][1] = 5 arr[3][2] = 6
  • 10.
    Storing elements ina matrix and printing it. 1. #include <stdio.h> 2. void main () 3. { 4. int arr[3][3],i,j; 5. for (i=0;i<3;i++) 6. { 7. for (j=0;j<3;j++) 8. { 9. printf("Enter a[%d][%d]: ",i,j); 10. scanf("%d",&arr[i][j]); 11. } 12. } 13. printf("n printing the elements ....n"); 14. for(i=0;i<3;i++) 15. { 16. printf("n"); 17. for (j=0;j<3;j++) 18. { 19. printf("%dt",arr[i][j]); 20. } 21. } 22. } Output Enter a[0][0]: 56 Enter a[0][1]: 10 Enter a[0][2]: 30 Enter a[1][0]: 34 Enter a[1][1]: 21 Enter a[1][2]: 34 Enter a[2][0]: 45 Enter a[2][1]: 56 Enter a[2][2]: 78 printing the elements .... 56 10 30 34 21 34 45 56 78
  • 11.
    Passing Array toFunction in C Consider the following syntax to pass an array to the function.  functionname(arrayname);//passing array Methods to declare a function that receives an array as an argument There are 3 ways to declare the function which is intended to receive an array as an argument. 1.First way:  return_type function(type arrayname[])  Declaring blank subscript notation [] is the widely used technique. 2.Second way:  return_type function(type arrayname[SIZE])  Optionally, we can define size in subscript notation []. 3.Third way:  return_type function(type *arrayname)
  • 12.
    C language passingan array to function example 1. #include<stdio.h> 2. int minarray(int arr[],int size){ 3. int min=arr[0]; 4. int i=0; 5. for(i=1;i<size;i++){ 6. if(min>arr[i]){ 7. min=arr[i]; 8. } 9. }//end of for 10. return min; 11. }//end of function 12. 13. int main(){ 14. int i=0,min=0; 15. int numbers[]={4,5,7,3,8,9};//declaration of array 16. 17. min=minarray(numbers,6);//passing array with size 18. printf("minimum number is %d n",min); 19. return 0; 20. } Output minimum number is 3
  • 13.
    C function tosort the array 1. #include<stdio.h> 2. void Bubble_Sort(int[]); 3. void main () 4. { 5. int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; 6. Bubble_Sort(arr); 7. } 8. void Bubble_Sort(int a[]) //array a[] points to arr. 9. { 10. int i, j,temp; 11. for(i = 0; i<10; i++) 12. { 13. for(j = i+1; j<10; j++) 14. { 15. if(a[j] < a[i]) 16. { 17. temp = a[i]; 18. a[i] = a[j]; 19. a[j] = temp; 20. } 21. } 22. } 23. printf("Printing Sorted Element List ...n"); 24. for(i = 0; i<10; i++) 25. { 26. printf("%dn",a[i]); 27. } 28. } Output Printing Sorted Element List ... 7 9 10 12 23 23 34 44 78 101
  • 14.
  • 15.
    C Strings  Thestring can be defined as the one-dimensional array of characters terminated by a null ('0').  The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0.  The termination character ('0') is important in a string since it is the only way to identify where the string ends.  When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
  • 16.
    There are twoways to declare a string in c language. 1.By char array 2.By string literal Let's see the example of declaring string by char array in C language. 1. char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0' }; As we know, array index starts from 0, so it will be represented as in the figure given below.
  • 17.
     While declaringstring, size is not mandatory. So we can write the above code as given below:  char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0'};  We can also define the string by the string literal in C language. For example:  char ch[]="javatpoint";  In such case, '0' will be appended at the end of the string by the compiler. Difference between char array and string literal: There are two main differences between char array literal and. We need to add the null character '0' at the end of the array by ourself whereas, it is appended internally by the compiler in the case of the character array. The string literal cannot be reassigned to another set
  • 18.
    Example program: #include<stdio.h> #include <string.h> intmain(){ char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '0'}; char ch2[11]="javatpoint"; printf("Char Array Value is: %sn", ch); printf("String Literal Value is: %sn", ch2); return 0; } Output: Char Array Value is: javatpoint String Literal Value is: javatpoint
  • 19.
    Traversing String: Hence, thereare two ways to traverse a string. 1. By using the length of string 2. By using the null character. Using the length of string: Let's see an example of counting the number of vowels in a string. #include<stdio.h> void main () { char s[11] = "javatpoint"; int i = 0; int count = 0; while(i<11) { if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count ++; } i++; } printf("The number of vowels %d",count); } Output The number of vowels 4
  • 20.
    Using the nullcharacter: Let's see the same example of counting the number of vowels by using the null character. #include<stdio.h> void main () { char s[11] = "javatpoint"; int i = 0; int count = 0; while(s[i] != NULL) { if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count ++; } i++; } printf("The number of vowels %d",count); } Output: The number of vowels 4
  • 21.
    C gets() andputs() functions:  The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings. C gets() function:  The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user. Declaration  char[] gets(char[]); Reading string using gets(): #include<stdio.h> void main () { char s[30]; printf("Enter the string? "); gets(s); printf("You entered %s",s); } Output: Enter the string? javatpoint is the best You entered javatpoint is the best
  • 22.
    C puts() function: The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1. Declaration:  int puts(char[]) Let's see an example to read a string using gets() and print it on the console using puts(). #include<stdio.h> #include <string.h> int main(){ char name[50]; printf("Enter your name: "); gets(name); //reads string from user printf("Your name is: "); puts(name); //displays string return 0; } Output: Enter your name: Sonoo Jaiswal
  • 23.
    C String Functions: There are many important string functions defined in "string.h" library. No. Function Description 1) strlen(string_name) returns the length of string name. 2) strcpy(destination, source) copies the contents of source string to destination string. 3) strcat(first_string, second_string) concats or joins first string with second string. The result of the string is stored in first string. 4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0. 5) strrev(string) returns reverse string. 6) strlwr(string) returns string characters in lowercase. 7) strupr(string) returns string characters in uppercase.