C Programming - Arrays and Strings
C Programming - Arrays and Strings
In C programming, arrays and strings are used to store and manipulate collections of data. Arrays can
store multiple values of the same data type, while strings are specialized character arrays used to store
text.
1. Arrays in C
An array is a collection of elements of the same data type, stored in contiguous memory locations.
CopyEdit
data_type array_name[size];
Example:
CopyEdit
Types of Arrays
1. One-Dimensional Arrays
1. One-Dimensional Arrays
CopyEdit
CopyEdit
printf("%d", numbers[2]); // Output: 30
CopyEdit
#include <stdio.h>
int main() {
return 0;
Output:
CopyEdit
12345
2. Multi-Dimensional Arrays
CopyEdit
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing Elements:
c
CopyEdit
CopyEdit
#include <stdio.h>
int main() {
printf("\n");
return 0;
Output:
CopyEdit
12
34
CopyEdit
int main() {
printArray(numbers, 3);
return 0;
Output: 10 20 30
Insertion
Deletion
Searching
Sorting
CopyEdit
#include <stdio.h>
return -1;
}
int main() {
if (index != -1)
else
return 0;
2. Strings in C
CopyEdit
char str3[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Manual initialization
CopyEdit
#include <stdio.h>
int main() {
char name[20];
return 0;
CopyEdit
#include <stdio.h>
int main() {
char sentence[100];
puts(sentence);
return 0;
CopyEdit
#include <stdio.h>
#include <string.h>
int main() {
return 0;
}
Output: Length: 11
CopyEdit
char destination[20];
strcpy(destination, source);
CopyEdit
strcat(str1, str2);
CopyEdit
if (strcmp("apple", "banana") == 0)
else
CopyEdit
CopyEdit
#include <stdio.h>
#include <string.h>
int main() {
gets(str1);
gets(str2);
if (strcmp(str1, str2) == 0)
else
strcat(str1, str2);
return 0;
CopyEdit
int main() {
printString(name);
return 0;
1. Reversing a String
2. Checking Palindrome
3. Counting Characters
CopyEdit
#include <stdio.h>
#include <string.h>
return 0;
}
return 1;
int main() {
if (isPalindrome(str))
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;