Unit 3 Arrays and Strings
Unit 3 Arrays and Strings
Types
One-Dimensional array
Two-Dimensional array
Multi-Dimensional array
Array declaration:
Syntax:
data_type array_name[size];
Example: int x[3];
x
X[0]
X[1]
PREETHA V AP/CSE, SRIT
X[2]
Array initialization
The initializer for an array is a comma-separated list of constant
expressions enclosed in braces ({ }).
Syntax:
data_type array_name[size]={variables};
Example: int x[3]={5,3,7};
x
5
X[0]
3
X[1]
7
PREETHA V AP/CSE, SRIT
X[2]
At Run time
Example:
while(i<10)
{
if(i<5)
sum[i]=0;
else
sum[i]=sum[i]+i;
}
PREETHA V AP/CSE, SRIT
Example:
scanf(“%d%d”,&a[0],&a[1]);
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int x[2],i;
printf("\nEnter the inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("\nThe value in x[%d] is %d",i,x[i]);
PREETHA V AP/CSE, SRIT getch();
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
printf("\nThe value in x[%d] is %c",i,x[i]);
PREETHA V AP/CSE, SRIT
getch();
}
Output
Array declaration
Syntax:
data_type array_name[row_size] [col_size];
Example: int x[3][2];
Col 0 Col 1
row 0 X[0][0] X[0][1]
Syntax:
data_type array_name[row_size] [col_size]={variables};
(or)
Col 0 Col 1
1 50
row 0
2 75
row 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<2;j++)
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
PREETHA V AP/CSE, SRIT
}
Output
a b c \0
a b c d
int a[3][3][3];
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
strlen()
It is used to find the length of the string.
syntax:
strlen(string)
strcpy()
It is used to copy one string to another.
syntax:
strcpy(string1,string2)
strcat()
It is used to combine two strings.
syntax:
PREETHA V AP/CSE, SRIT
strcat(string1,string2)
strcmp()
It is used to compare two strings.
syntax:
strcmp(string1,string2)
Returns 0 if two strings are equal.
Return value <0 if s1 is less than s2.
Return value >0 if s1 is greater than s2.
strrev()
It used to reverse a string.
syntax:
strrev(string)
strlwr(), strupr()
It used to change the case of a string.
syntax:
PREETHA V AP/CSE, SRIT strlwr(string)
strupr(string)
strncpy()
It used to copy ‘n’ characters of one string to another.
strstr()
It is used to determine the first occurrence of a given string in another string.
strncat()
It appends source string to destination string up to specified length.
strspn()
It is used to find up to what length two strings are identical.
strcmpi()
It is used to compare two strings without regarding the case.
strnicmp()
It is used to compare first ‘n’ characters of two strings
without regarding the case.
strchr()
It is used to determine the first occurrence of a given
character in a string.
Output:
PREETHA V AP/CSE, SRIT
The string is Dept
Example 3
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="IT";
char b[ ]="Dept";
clrscr();
strcat(a,b);
printf("\nThe string is %s",a);
getch();
}
Output:
PREETHA V AP/CSE, SRIT
The string is ITDept
Example 4
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="itdept";
char b[ ]="it";
int i;
clrscr();
i=strcmp(a,b);
if(i==0)
printf("\nstrings are equal:%d",i);
PREETHA V AP/CSE, SRIT
else if(i<0)
printf("\nstring1 is less than string2:%d",i);
else
printf("\nstring1 is greater than string2:%d",i);
getch();
}
Output:
string1 is greater than string2:100
Output:
PREETHA V AP/CSE, SRIT The string is tpeD
Example 7
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[ ]="itdept";
char b[15];
int i=0;
clrscr();
strncpy(b,a,2);
b[2]='\0';
printf("\nThe string is :%s",b);
getch();
}
PREETHA V AP/CSE, SRIT
Output:
The string is :it
String Palindrome
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int len,i,j;
char str[15];
clrscr();
printf("\n Enter the string:");
scanf("%s",str);
len=strlen(str);
PREETHA V AP/CSE, SRIT
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(str[i]!=str[j])
{
printf("\n The String is not a palindrome");
getch();
exit(0);
}
}
printf("\n The String is a palindrome");
getch();
}
Output:
PREETHA V AP/CSE, SRIT
Enter the string: abcba
The String is a palindrome
PREETHA V - AP/CSE, SRIT.