05 Array - String GUI
05 Array - String GUI
– If the size of an array is n, to access the last element, the n-1 index is
used. In this example, mark[4]
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
Enter 5 integers: 1
-3
3
4
0
3
Displaying integers: 1
-3
3
4
0
3
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
• Initialization of a 2D array
• Initialization of a 3D array
int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5,
9, 3, 5}, {3, 1, 4, 9}}};
i=sizeof(myArray)/sizeof(myArray[0]);
• Size of a 2D array : int x[i][j];
i=sizeof(myArray)/sizeof(myArray[0]);
j=sizeof(myArray[0])/sizeof(myArray[0][0]);
• Size of a 3D array : int x[i][j][k];
i=sizeof(myArray)/sizeof(myArray[0]);
j=sizeof(myArray[0])/sizeof(myArray[0][0]);
k= sizeof(myArray[0][0])/sizeof(myArray[0][0][0]);
19 September 2022 Programming for Engineers (C language) NV Binh 14
3 Array - String
3.3 String
• String Arrays:
char label[] = ”Hello world"; // 12 characters element
H e l l o w o r l d \0
char label[15] = ”Hello world"; // 15 characters element
H e l l o w o r l d \0
Or:
char text[20];
gets(text);
printf("%s", text);
19 September 2022 Programming for Engineers (C language) NV Binh 16
3 Array - String
3.3 String
String Handling Functions
#include<string.h>
Method Description
strcat() It is used to concatenate(combine) two
strings
strlen() It is used to show length of a string
strrev() It is used to show reverse of a string
strcpy() Copies one string into another
strcmp() It is used to compare two string
19 September 2022 Programming for Engineers (C language) NV Binh 17
3 Array - String
3.3 String
String Handling Functions
• strcat() function
Strcat("hello", "world");
• strlen() function
int j;
j = strlen("studytonight");
printf("%d",j);
Output: 12
• strcmp() function
int j;
j = strcmp("study", "tonight");
printf("%d",j);
• Providing more initializers in an array initializer list than there are elements
in the array is a syntax error.
–int value[3]={1,2,3,4};
• Not providing scanf with a character array large enough to store a string
typed at the keyboard