Array v/s String in C
Array v/s String
Array is defined as a contiguous block of memory in which similar type of data is stored. Array indexing starts with size 0 and ends with size -1. String is defined as a sequence of character which terminates with a null character.
Array v/s String Declaration in C:
Array Declaration
[arr_size]={value 1, value 2, value 3,…,value n-1};
String Declaration
char str[] = "string_name";
Difference between Array and String
Array | String |
Array is a collection of elements of similar data type. | Strings are a sequence of character of a single data type. |
Array can store a set of integer, character, float etc. | String can store a set of characters only. |
Array has a fixed size and it cannot be changed after declaration. | String has fixed size but it can be changed using char pointer. |
Arrays don’t need to end with the null character. | String ends with the null character. |
Array can be one dimensional, two dimensional and multidimensional. | String are always two dimensional. |
Array: Example 1
Run
#include <stdio.h> int main() { int values[6] = {2,4,6,8,4,9}; printf("Displaying integers: "); for(int i = 0; i < 6; ++i) { printf("%d ", values[i]); } return 0; }
Output
Displaying integers: 2 4 6 8 4 9
Array: Example 2
#include<stdio.h> int main () { int i, j, temp; int a[15] = { 15, 33, 67, 43, 89, 11, 99, 56, 55, 46, 78, 93, 22, 44, 66 }; for (i = 0; i < 15; i++) { for (j = i + 1; j < 15; j++) { if (a[j] > a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf ("Sorted Elements are = \n"); for (i = 0; i < 15; i++) { printf ("%d ", a[i]); } return 0; }
Output
Sorted Elements are = 99 93 89 78 67 66 56 55 46 44 43 33 22 15 11
Array: Example 3
Run
#include<stdio.h> int main () { int i, largest, sec_largest; int arr[10] = { 2, 1, 5, 4, 87, 76, 89, 99, 80, 10 }; largest = arr[0]; sec_largest = arr[1]; for (i = 0; i < 10; i++) { if (arr[i] > largest) { sec_largest = largest; largest = arr[i]; } else if (arr[i] > sec_largest && arr[i] != largest) { sec_largest = arr[i]; } } printf ("largest = %d\nSecond largest = %d", largest, sec_largest); return 0; }
Output
largest = 99 Second largest = 89
String: Example 1
Run
#include<stdio.h> int main () { char s[10] = "PrepInsta"; int i = 0; int count = 0; while (i < 10) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o' || s[i]== 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U') { count++; } i++; } printf ("The number of vowels = %d", count); return 0; }
Output
The number of vowels = 3
String: Example 2
#include<stdio.h> int main () { char s[9] = "atsnIperP"; // declaration of string printf ("The reverse string is :"); for (int i = 8; i >= 0; i--) { printf ("%c", s[i]); } return 0; }
Output
The reverse string is :PrepInsta
String: Example 3
Run
#include <stdio.h> int main () { char s[10] = "PrepInsta"; int i = 0; int count = 0; while (i < 10) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U') { printf ("The first vowel in the string : %c", s[i]); return 0; } i++; } printf ("There is no vowel in the string"); return 0; }
Output
The first vowel in the string : e
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment