Array
Array is the collection of multiple values in a single variable stored in a contiguous memory location. In array, we can
access the element of array by using its index. Array cannot contain dis-similar type of data. The size of array is
always begins with zero index.
Eg: int a (5);// array declaration
Output:
0=2;
1=4;
2=8;
3=12;
4=16;
5=18;
Features of array:
- Array holds the element of the same data type only.
- Array size should be mentioned in the declaration and should always be a positive number.
- The element of an array can be accessed using an index value.
- The size of an array is equivalent to the number of element in the array.
- Two-dimensional array elements are stored row by row in subsequent memory locations.
Types of array:
- One-dimensional array:
The array having only one subscript is called one-dimensional array. Syntax:
Data_type array_name[size];
Initialization a one-dimensional array:
An array can be initialization during the time of declaration as follows: Int a[5]={1, 2, 3, 4, 5};
Here, the array name is ‘a’ and initializes its elements where a[0] is initialized with1, a[1] is initialized with 2, a[2] is initialized with 3, a[3] is initialized with 4, and a[4] is initialized with 5.
Eg:
#include <stdio.h>
int main() {
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;
}
- Two-dimensional array:
The array having two subscripts is called two-dimensional array. 2D arrays are used in matrix-related problem.
Syntax:
Char string_variable[string_size];
Declaration of string
String
string refers to the sequence of character placed one after another and terminated with a null character ‘\0’. C provides a standard library <string.h> which contains functions to perform different operations on string data.
Syntax:
Char string_variables[string_size];
Initialization of string
The initialization of a string is similar to a one dimensional array Let us take a string “computer”,
Char name[]= “computer”;
Char name[9]= {‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’, ‘\0 ‘};
Char name[9]= “computer”;
Array of string
The two-dimensional array of computer is called array of strings. It is used for solving strings sorting problems. The handling is similar two-dimensional arrays.
Syntax:
Char string_variable[no][size];
Eg:
#include<stdio.h>
Void main()
{
Char name[20];
Printf(“enter name:”);
Scanf(“%s”, &name);
}
String handling functions
C provides a set of pre-define function to work with the string data called string handling functions. The string handling functions are used to manipulate string data and their functionalities are defined in a header file.
- Strlen()
The strlen() function returns the length of a string. It is a function that counts the number of characters in a string. The functions does not count the null.
Syntax:
Eg:
#include<stdio.h>
#include<string.h>
Void main()
{
Char s[20];
Int x;
Printf(“enter a string:”);
Scanf(“%s”, &s);
X=strlen(s);
Printf(“length of the string is: %d”, x);
}
- Strcpy()
Strcpy() is used to copy the contents of a string to another, the data of source_variable is copied to destination_variable.
Syntax:
Strcpy(destination_variable, source_variable);
Eg:
#include<stdio.h>
#include<string.h>
Void main()
{
Char a[30], b[30];
Printf(“enter a string:”);
Scanf(“%s”, &a);
Strcpy(b, a);
Printf(“value of a: %s”,a);
Printf(“\n value of b: %s”, b);
}
- Strcat()
This function is used to join the two strings resulting in a single string. It takes two arguments, which are destination and source strings. The destination and source string are concatenated and resultant is stored in the destination string.
Syntax:
Strcat(a, b);
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char name[50];
char message[100] = “Hello, “;
printf(“Enter your name: “);
scanf(“%s”, name);
strcat(message, name);
printf(“Final message: %s\n”, message);
return 0;
}
- Strcmp()
Strcmp() function is used to compare two string by character by character and stops comparison when there is difference in ASCII value or the end of any one string and returns ASCII difference of the characters that is integer.
Syntax:
Strcmp(a, b);
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
char storedName[] = “John”;
printf(“Enter a name: “);
scanf(“%s”, name);
int result = strcmp(name, storedName);
if (result == 0)
{
}
else
{
printf(“Entered name does not match stored name.\n”);
}
return 0;
}
- Strrev()
strrev() function is used to reverse the characters the characters in a string.
Syntax:
Strrev(a);
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
printf(“Enter string: “);
strrev(string);
printf(“Reversed string: %s\n”, string);
return 0;
}
- Strlwr()
Strlwr() function is used to convert uppercase characters in a string into lowercase.
Syntax:
Strlwr(a);
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
printf(“Enter a name in uppercase: “);
scanf(“%s”, &a);
printf(“Lowercase %s\n”, strlwr(a));
return 0;
}
- Strupr()
Strupr() function is used to convert uppercase characters in a string into lowercase.
Syntax:
Strupr(a);
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
printf(“Enter a name in lowercase: “);
scanf(“%s”, &a);
printf(“uppercase %s\n”, strlwr(a));
return 0;
}