Arrays and Strings
Arrays and Strings
Intoduction
• C Array is a collection of variables belongings to
the same data type. You can store group of data
of same data type in an array.
• Array might be belonging to any of the data types
• Array size must be a constant value.
• Always, Contiguous (adjacent) memory locations
are used to store array elements in memory.
• Eg.
int a[10];
Types of Arrays
• There are 2 types of arrays in C. They are,
• One dimensional array
• Multi dimensional array
– Two dimensional array
– Three dimensional array
– four dimensional array etc…
PROGRAM FOR 1-D ARRAY IN C
#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}
Sorting a 1-D Array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("Enter elements\n");
for (i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Sorted array is\n");
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
printf("%d",a[i]);
}
TWO DIMENSIONAL ARRAY IN C:
• Two dimensional array is nothing but array of
array.
• syntax : data_type array_name[num_of_rows]
[num_of_column];
• Eg. int arr[2][2];
int arr[2][2] = {1,2, 3, 4};
OR
arr [0] [0] = 1;
arr [0] ]1] = 2;
arr [1][0] = 3;
arr [1] [1] = 4;
PROGRAM FOR 2-D ARRAY IN C:
#include<stdio.h>
main()
{ int i,j;
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{ printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]); }
}
}
Strings
• Strings are actually one-dimensional array of
characters terminated by a null character '\0'.
• Eg.
char msg[6];
char msg[6]={‘H’, ’e’, ’l’, ’l’, ’o’, ’\0’};
char msg[]= “Hello”;
Index 0 1 2 3 4 5
H e l l o \0
• In C, string can be initialized in a number of
different ways.
char c[] = "abcd";
OR
char c[5] = "abcd";
OR
char c[] = {'a', 'b', 'c', 'd', '\0'};
OR
char c[5] = {'a', 'b', 'c', 'd', '\0'};
String Input
• Using scanf() : You can use the scanf() function to
read a string like any other data types.
• The scanf() function only takes the first entered
word. The function terminates when it encounters a
space.
Eg. char str[10];
scanf(“%s”, str);
• Using gets() : there is a predefined functions gets()
to read string
Eg. gets(str);
String Output
• Using printf() :
Eg.
printf(“%s”, str);
• Using puts() :
Eg.
puts(str);
• Eg.
#include <stdio.h>
int main()
{
char name[20]; printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
• Eg. Using gets(), puts() –
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
printf("Name: ");
puts(name);
return 0;
}
Std. Library Functions for strings
• C supports a wide range of functions that
manipulate null-terminated strings
Sr.No. Function & Purpose
strcpy(s1, s2);
1
Copies string s2 into string s1.
strcat(s1, s2);
2
Concatenates string s2 onto the end of string s1.
strlen(s1);
3
Returns the length of string s1.
strcmp(s1, s2);
4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2.
• Eg.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12]; int len ;
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation
*/
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret < 0)
{ printf("str1 is less than str2"); }
else if(ret > 0)
{ printf("str2 is less than str1"); }
else { printf("str1 is equal to str2"); }
return(0);
}