Arrays and Strings in C Programming
Array
An array is a collection of multiple values of the same data type stored in contiguous memory locations under a single variable name.
Each element of an array can be accessed using its index number.
In C, array indexing always begins from 0.
For example, if an array has 5 elements, its indexes are:
0, 1, 2, 3, 4
Therefore, an array of size 5 contains exactly five elements.
Array Declaration
Syntax
data_type array_name[size];
Example
int a[5];
Here:
int→ Data typea→ Array name5→ Number of elements
The elements are:
a[0]
a[1]
a[2]
a[3]
a[4]
Features of Array
The important features of an array are:
- An array can store multiple values under a single variable name.
- All elements of an array must normally be of the same data type.
- Array elements are stored in contiguous memory locations.
- Each element is accessed using an index.
- Array indexing begins from
0. - Arrays make it easier to process a large number of similar values.
- Array elements can be processed efficiently using loops.
Types of Array
The main types of arrays are:
- One-Dimensional Array
- Two-Dimensional Array
1. One-Dimensional Array
An array having only one subscript or index is called a one-dimensional array.
It is used to store a list of values of the same data type.
Syntax
data_type array_name[size];
Example
int marks[5];
Initialization of a One-Dimensional Array
An array can be initialized at the time of declaration.
Example
int a[5] = {1, 2, 3, 4, 5};
Here:
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
Notice that although the size is 5, the last index is 4.
Another Method
The size may be omitted when values are provided during initialization.
int a[] = {1, 2, 3, 4, 5};
The compiler automatically determines that the array contains five elements.
Example of One-Dimensional Array
#include <stdio.h>
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
printf("Elements of the array:\n");
for (i = 0; i < 5; i++)
{
printf("%d\n", numbers[i]);
}
return 0;
}
Output
Elements of the array:
10
20
30
40
50
Accessing Array Elements
Array elements are accessed using their indexes.
For example:
int a[5] = {2, 4, 8, 12, 16};
The elements can be accessed as:
a[0] = 2
a[1] = 4
a[2] = 8
a[3] = 12
a[4] = 16
2. Two-Dimensional Array
An array having two subscripts or indexes is called a two-dimensional array.
A two-dimensional array is commonly used to represent:
- Tables
- Rows and columns
- Matrices
Syntax
data_type array_name[rows][columns];
Example
int matrix[2][3];
This creates a matrix containing:
- 2 rows
- 3 columns
- 6 total elements
Initialization of Two-Dimensional Array
A two-dimensional array can be initialized as follows:
int matrix[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
It represents:
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
Example of Two-Dimensional Array
#include <stdio.h>
int main(void)
{
int matrix[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
1 2 3
4 5 6
String
A string is a sequence of characters stored one after another and terminated by a special null character:
\0
In C, strings are generally stored using character arrays.
The <string.h> header file provides several functions for performing operations on strings.
Declaration of String
Syntax
char string_name[size];
Example
char name[20];
This creates a character array that can store a string.
Initialization of String
The initialization of a string is similar to the initialization of a one-dimensional character array.
Consider the word:
computer
It can be initialized in different ways.
Method 1
char name[] = "computer";
Method 2
char name[9] =
{
'c', 'o', 'm', 'p',
'u', 't', 'e', 'r',
'\0'
};
Method 3
char name[9] = "computer";
The extra character is required for the terminating null character \0.
How a String is Stored
For:
char name[] = "computer";
the characters are stored as:
| Index | Character |
|---|---|
| 0 | c |
| 1 | o |
| 2 | m |
| 3 | p |
| 4 | u |
| 5 | t |
| 6 | e |
| 7 | r |
| 8 | \0 |
Input and Output of a String
A string can be read using scanf() for simple single-word input.
Example
#include <stdio.h>
int main(void)
{
char name[20];
printf("Enter name: ");
scanf("%19s", name);
printf("Your name is: %s\n", name);
return 0;
}
Array of Strings
An array of strings is used to store several strings under a single array name.
In C, an array of strings can be represented using a two-dimensional character array.
Syntax
char string_name[number_of_strings][size_of_each_string];
Example
char names[3][20];
This can store three strings, with space for up to 19 characters plus the null character in each row.
Example of Array of Strings
#include <stdio.h>
int main(void)
{
char names[3][20] =
{
"Ram",
"Sita",
"Hari"
};
int i;
for (i = 0; i < 3; i++)
{
printf("%s\n", names[i]);
}
return 0;
}
Output
Ram
Sita
Hari
Arrays of strings can be useful in operations such as:
- Storing names
- Searching strings
- Sorting strings
- Creating lists of words
String Handling Functions
C provides several predefined functions for manipulating strings.
Most standard string handling functions are declared in:
#include <string.h>
Important string handling functions include:
strlen()strcpy()strcat()strcmp()
Some older compilers also provide:
strrev()strlwr()strupr()
1. strlen()
The strlen() function is used to find the length of a string.
It counts the number of characters before the terminating null character \0.
The null character itself is not counted.
Syntax
strlen(string);
Example of strlen()
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[20];
int length;
printf("Enter a string: ");
scanf("%19s", s);
length = strlen(s);
printf("Length of the string is: %d\n", length);
return 0;
}
Example
If the input is:
Nepal
Output:
Length of the string is: 5
2. strcpy()
The strcpy() function is used to copy one string into another string.
The contents of the source string are copied to the destination string.
Syntax
strcpy(destination, source);
Example of strcpy()
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[30];
char b[30];
printf("Enter a string: ");
scanf("%29s", a);
strcpy(b, a);
printf("Value of a: %s\n", a);
printf("Value of b: %s\n", b);
return 0;
}
Example Output
Enter a string: Nepal
Value of a: Nepal
Value of b: Nepal
3. strcat()
The strcat() function is used to join or concatenate two strings.
It appends the source string to the end of the destination string.
The final result is stored in the destination string.
Syntax
strcat(destination, source);
Example of strcat()
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[50];
char message[100] = "Hello, ";
printf("Enter your name: ");
scanf("%49s", name);
strcat(message, name);
printf("Final message: %s\n", message);
return 0;
}
Example Output
Enter your name: Ram
Final message: Hello, Ram
4. strcmp()
The strcmp() function is used to compare two strings.
The comparison is performed character by character.
Syntax
strcmp(string1, string2);
The function returns:
0if both strings are equal.- A value less than
0if the first string comes before the second. - A value greater than
0if the first string comes after the second.
Example of strcmp()
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[100];
char storedName[] = "John";
int result;
printf("Enter a name: ");
scanf("%99s", name);
result = strcmp(name, storedName);
if (result == 0)
{
printf("Entered name matches the stored name.\n");
}
else
{
printf("Entered name does not match the stored name.\n");
}
return 0;
}
5. strrev()
The strrev() function is used in some compilers to reverse the characters of a string.
Syntax
strrev(string);
Example
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[100];
printf("Enter a string: ");
scanf("%99s", string);
strrev(string);
printf("Reversed string: %s\n", string);
return 0;
}
For example:
Input: Nepal
Output: lapeN
Note:
strrev()is not part of the ISO standard C library. It is available only in some compilers and environments.
6. strlwr()
The strlwr() function converts uppercase characters in a string into lowercase characters.
Syntax
strlwr(string);
Example
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[100];
printf("Enter a name in uppercase: ");
scanf("%99s", name);
strlwr(name);
printf("Lowercase: %s\n", name);
return 0;
}
Example
Input: NEPAL
Output: nepal
Note:
strlwr()is not part of the standard C library and may not be supported by modern compilers.
7. strupr()
The strupr() function converts lowercase characters in a string into uppercase characters.
Syntax
strupr(string);
Example
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[100];
printf("Enter a name in lowercase: ");
scanf("%99s", name);
strupr(name);
printf("Uppercase: %s\n", name);
return 0;
}
Example
Input: nepal
Output: NEPAL
Note:
strupr()is also a compiler-specific function and is not part of the ISO standard C library.
Difference Between strlen(), strcpy(), strcat() and strcmp()
| Function | Purpose |
|---|---|
strlen() | Finds the length of a string |
strcpy() | Copies one string to another |
strcat() | Joins two strings |
strcmp() | Compares two strings |
Additional String Functions
The <string.h> library also provides several other useful standard functions.
Examples
strncpy()
strncat()
strncmp()
strchr()
strstr()
These functions provide additional ways to copy, join, compare, and search strings.
Difference Between Array and String
| Array | String |
|---|---|
| An array stores multiple values of the same data type. | A string stores a sequence of characters. |
| It can be an array of integers, floats, characters, etc. | It is normally represented using a character array. |
| Elements are accessed using indexes. | Characters are also accessed using indexes. |
| An ordinary array does not require a null terminator. | A C string ends with the null character \0. |
Example: int a[5] | Example: char name[20] |
Quick Revision
Array
An array stores multiple values of the same data type using a single variable name.
Syntax
int a[5];
Array indexing starts from:
0
One-Dimensional Array
Contains one index.
int a[5];
Two-Dimensional Array
Contains two indexes.
int a[2][3];
It is commonly used for matrix and table-related problems.
String
A string is a sequence of characters terminated by:
\0
Example:
char name[] = "Nepal";
Array of Strings
A two-dimensional character array can store multiple strings.
char names[5][20];
Important String Functions
strlen() → Finds string length
strcpy() → Copies a string
strcat() → Joins strings
strcmp() → Compares strings
Compiler-specific functions commonly taught in older C environments include:
strrev() → Reverses a string
strlwr() → Converts to lowercase
strupr() → Converts to uppercase
Important Exam Points
- An array stores multiple values of the same data type.
- Array elements are stored in contiguous memory locations.
- Array indexing begins from
0. - An array of size
5has indexes from0to4. - A one-dimensional array has one subscript.
- A two-dimensional array has two subscripts.
- Two-dimensional arrays are commonly used to represent matrices.
- A string is a sequence of characters.
- Every valid C string ends with the null character
\0. - Strings are commonly represented using character arrays.
<string.h>contains declarations for standard string-handling functions.strlen()finds the length of a string.strcpy()copies one string to another.strcat()concatenates strings.strcmp()compares strings.strrev(),strlwr(), andstrupr()are not standard ISO C functions and may only be available in certain compilers.
Discussion
Share a helpful question, idea, or explanation with other students.