Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

C Programming - Arrays and Strings

The document provides an overview of arrays and strings in C programming, detailing their definitions, types, and operations. It explains how to declare and initialize one-dimensional and multi-dimensional arrays, along with common operations such as insertion, deletion, and searching. Additionally, it covers string manipulation, including declaration, input/output methods, and built-in string functions.

Uploaded by

06303648
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Programming - Arrays and Strings

The document provides an overview of arrays and strings in C programming, detailing their definitions, types, and operations. It explains how to declare and initialize one-dimensional and multi-dimensional arrays, along with common operations such as insertion, deletion, and searching. Additionally, it covers string manipulation, including declaration, input/output methods, and built-in string functions.

Uploaded by

06303648
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arrays and Strings in C Programming

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.

Syntax for Declaring an Array

CopyEdit

data_type array_name[size];

Example:

CopyEdit

int numbers[5]; // Declaration of an integer array of size 5

Types of Arrays

1. One-Dimensional Arrays

2. Multi-Dimensional Arrays (2D, 3D, etc.)

1. One-Dimensional Arrays

Declaration and Initialization:

CopyEdit

int numbers[5] = {10, 20, 30, 40, 50};

Accessing Array Elements:

Elements are accessed using an index, starting from 0.

CopyEdit
printf("%d", numbers[2]); // Output: 30

Example: Traversing an Array

CopyEdit

#include <stdio.h>

int main() {

int arr[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

return 0;

Output:

CopyEdit

12345

2. Multi-Dimensional Arrays

Arrays with more than one dimension, such as 2D arrays (matrices).

Declaration and Initialization:

CopyEdit

int matrix[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

Accessing Elements:
c

CopyEdit

printf("%d", matrix[1][2]); // Output: 6

Example: Traversing a 2D Array

CopyEdit

#include <stdio.h>

int main() {

int matrix[2][2] = {{1, 2}, {3, 4}};

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

return 0;

Output:

CopyEdit

12

34

3. Passing Arrays to Functions

Arrays can be passed to functions using their base address.

CopyEdit

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

int main() {

int numbers[] = {10, 20, 30};

printArray(numbers, 3);

return 0;

Output: 10 20 30

4. Common Operations on Arrays

 Insertion

 Deletion

 Searching

 Sorting

Example: Linear Search

CopyEdit

#include <stdio.h>

int search(int arr[], int size, int key) {

for (int i = 0; i < size; i++) {

if (arr[i] == key) return i;

return -1;

}
int main() {

int numbers[] = {5, 10, 15, 20, 25};

int index = search(numbers, 5, 15);

if (index != -1)

printf("Element found at index %d\n", index);

else

printf("Element not found\n");

return 0;

2. Strings in C

A string in C is an array of characters, terminated by a null character \0.

Declaration and Initialization of Strings

CopyEdit

char str1[] = "Hello"; // Implicit null character added

char str2[6] = "Hello"; // Explicitly specifying size

char str3[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Manual initialization

Input and Output of Strings

Using scanf and printf (Avoids spaces)

CopyEdit

#include <stdio.h>

int main() {

char name[20];

printf("Enter your name: ");


scanf("%s", name); // Reads until space

printf("Hello, %s!\n", name);

return 0;

Using gets and puts (Allows spaces)

CopyEdit

#include <stdio.h>

int main() {

char sentence[100];

printf("Enter a sentence: ");

gets(sentence); // Allows spaces

puts(sentence);

return 0;

String Functions (Using <string.h>)

C provides several built-in functions to manipulate strings.

1. strlen() – Get String Length

CopyEdit

#include <stdio.h>

#include <string.h>

int main() {

char str[] = "Programming";

printf("Length: %lu\n", strlen(str));

return 0;
}

Output: Length: 11

2. strcpy() – Copy Strings

CopyEdit

char source[] = "Hello";

char destination[20];

strcpy(destination, source);

3. strcat() – Concatenate Strings

CopyEdit

char str1[20] = "Hello, ";

char str2[] = "World!";

strcat(str1, str2);

4. strcmp() – Compare Strings

CopyEdit

if (strcmp("apple", "banana") == 0)

printf("Strings are equal\n");

else

printf("Strings are different\n");

5. strrev() – Reverse a String (Not standard)

CopyEdit

char str[] = "Hello";


strrev(str); // Reverse the string

Example Program: Working with Strings

CopyEdit

#include <stdio.h>

#include <string.h>

int main() {

char str1[50], str2[50];

printf("Enter first string: ");

gets(str1);

printf("Enter second string: ");

gets(str2);

if (strcmp(str1, str2) == 0)

printf("Strings are equal\n");

else

printf("Strings are different\n");

strcat(str1, str2);

printf("Concatenated String: %s\n", str1);

return 0;

Passing Strings to Functions


Strings can be passed to functions using character pointers.

CopyEdit

void printString(char str[]) {

printf("String: %s\n", str);

int main() {

char name[] = "Alice";

printString(name);

return 0;

Common Operations on Strings

1. Reversing a String

2. Checking Palindrome

3. Counting Characters

4. Converting Case (Upper/Lower)

Example: Check Palindrome

CopyEdit

#include <stdio.h>

#include <string.h>

int isPalindrome(char str[]) {

int len = strlen(str);

for (int i = 0; i < len / 2; i++) {

if (str[i] != str[len - i - 1]) {

return 0;
}

return 1;

int main() {

char str[] = "madam";

if (isPalindrome(str))

printf("Palindrome\n");

else

printf("Not a Palindrome\n");

return 0;

Summary of Arrays and Strings in C

Feature Arrays Strings

Type Homogeneous data collection Character array

Initialization int arr[3] = {1,2,3}; char str[] = "Hello";

Accessing arr[1] str[1]

Size Fixed Can vary with null character

Functions Custom loops Built-in (strlen, strcpy)

You might also like