0% found this document useful (0 votes)
3 views52 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, explaining their declaration, initialization, and usage through examples. It covers various operations such as input, output, and manipulating arrays, including character arrays and strings. Additionally, it introduces library functions for string manipulation and exercises for practical application.

Uploaded by

shahriarabid4103
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views52 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, explaining their declaration, initialization, and usage through examples. It covers various operations such as input, output, and manipulating arrays, including character arrays and strings. Additionally, it introduces library functions for string manipulation and exercises for practical application.

Uploaded by

shahriarabid4103
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array

Array is a list of same type variables which can be


accessed through a common name. an individual
variable in the array is called an array element.

Array Declaration:
Data_type var_name[array_size];

array_size specifies the number of elements in the array.

1
Array(Cont.)

Example:
int var[10];
float venus[20];
double earth[5];
char pluto[7];

2
Array(Cont.)

int var[7];
0 1 2 3 4 5 6

var[0] - 1st element, index 0


var[1] - 2nd element, index 1
var[2] - 3rd element, index 2
var[3] - 4th element, index 3
var[4] - 5th element, index 4
var[5] - 6th element, index 5
var[6] - 7th element, index 6
3
Array(Cont.)

int var[7];
0 1 2 3 4 5 6
14 22 31 18 19 7 23

var[0] = 14;
var[6] = 23;
var[5] = 7;
var[3] = 18;
var[4] = 19;
var[1] = 22;
var[2] = 31;
4
Array initialize

int var[7] = {14, 22, 31, 18, 19, 7, 23};


int var[ ] = {14, 22, 31, 18, 19, 7, 23};
0 1 2 3 4 5 6
14 22 31 18 19 7 23

int var[7] = {14, 22, 31};


0 1 2 3 4 5 6
14 22 31 0 0 0 0

5
Array initialize(Cont.)

int var[7] = {9, 14, 0};


0 1 2 3 4 5 6
9 14 0 0 0 0 0

int var[7] = {9};


0 1 2 3 4 5 6
9 0 0 0 0 0 0

int var[7] = {0};


0 1 2 3 4 5 6
0 0 0 0 0 0 0 6
Array(Cont.)
#include<stdio.h>
void main()
{
int var[4];
var[0] = 15;
var[1] = 13;
var[2] = 17;
var[3] = 19;
printf(“%d”, var[0]);
printf(“%d”, var[1]);
printf(“%d”, var[2]);
printf(“%d”, var[3]);
} 7
Array(Cont.)
#include<stdio.h>
void main()
{
int var[4];

scanf(“ %d %d %d %d”, &var[0], &var[1], &var[2], &var[3]);

printf(“%d %d ”, var[0], var[1]);


printf(“%d %d ”, var[2], var[3]);
}

8
Array(Cont.)
#include<stdio.h>
void main()
{
int var[4];

scanf(“ %d”, &var[0]);


scanf(“ %d”, &var[1];
scanf(“ %d”, &var[2]);
scanf(“ %d”, &var[3]);

printf(“%d %d ”, var[0], var[1]);


printf(“%d %d ”, var[2], var[3]);
} 9
Array(Cont.)
#include<stdio.h>
void main()
{
int i, var[4];

for(i=0 ; i<4 ; i++ )


scanf(“ %d”, &var[i]);

printf(“%d %d %d %d”, var[0], var[1], var[2], var[3]);


}

10
Array(Cont.)
#include<stdio.h>
void main()
{
int var[4], i;

for(i=0 ; i<4 ; i++ )


scanf(“ %d”, &var[i]);

for(i=0 ; i<4 ; i++ )


printf(“ %d ”, var[i]);
}

11
Array(Cont.)
#include<stdio.h>
void main()
{
int var[100], i, n;

printf(“Number of Inputs: ”);


scanf(“ %d”, &n);

for(i=0 ; i<n ; i++ )


scanf(“ %d”, &var[i]);

for(i=0 ; i<n ; i++ )


printf(“ %d ”, var[i]);
} 12
Array(Cont.)
#include<stdio.h>
void main()
{ int var[100], i, n;
float avg=0;
printf(“Number of Inputs: ”);
scanf(“ %d”, &n);
for(i=0 ; i<n ; i++ )
scanf(“ %d”, &var[i]);

for(i=0 ; i<n ; i++ )


avg = avg + var[i]);

avg = avg / n;
printf(“ %f ”, avg); 13
}
Array(Cont.)
#include<stdio.h>
void main()
{ int var[100], i, n, min;

printf(“Number of Inputs: ”);


scanf(“ %d”, &n);
for(i=0 ; i<n ; i++ )
scanf(“ %d”, &var[i]);

min = var[0];
for(i=0 ; i<n ; i++ )
if(var[i] < min)
min = var[i];
printf(“ %d ”, min); 14
}
Array as a parameter
void ToPrint(int num[ ])
{
int i;
for(i=0 ; i<10 ; i++)
printf(“ %d”, num[i]);
}
void main()
{
int var[10], i;
for(i=0 ; i<10 ; i++)
scanf(“ %d”, &var[i]);
ToPrint(var);
} 15
Array as a parameter(Cont.)
void ToPrint (int num[ ], int n)
{
int i;
for(i=0 ; i<n ; i++)
printf(“ %d”, num[i]);
}
void main()
{ int var[10], n, i;
scanf(“ %d”, &n);
for(i=0 ; i<n ; i++)
scanf(“ %d”, &var[i]);
ToPrint(var, n);
} 16
Example
#include<stdio.h>

void main()
{
char x = ‘A’;
printf(“%d”, x);
}

17
Example
#include<stdio.h>

void main()
{
char x = 65;
printf(“%c”, x);
}

18
Example
#include<stdio.h>

void main()
{
char x;
scanf(“ %c”, &x);

printf(“%d”, x);
}

19
Example
#include<stdio.h>

void main()
{ char x;
scanf(“ %c”, &x);

if(x >= 97)


x = x – 32;
else
x = x + 32;

printf(“%c”, x);
} 20
Example
#include<stdio.h>

void main()
{ char x;
scanf(“ %c”, &x);

if(x >= ‘a’)


x = x – 32;
else
x = x + 32;

printf(“%c”, x);
} 21
Example
#include<stdio.h>

void main()
{ char x;
scanf(“ %c”, &x);

if(x <= ‘Z’)


x = x + 32;
else
x = x - 32;

printf(“%c”, x);
} 22
Example
#include<stdio.h>

void main()
{ char x;
scanf(“ %c”, &x);

if(x <= ‘Z’)


x = x + (‘a’ – ‘A’);
else
x = x – (‘a’ – ‘A’);

printf(“%c”, x);
} 23
Array as a parameter(Cont.)
void to_print(char ch[ ], int n)
{
int i;
for(i=0 ; i<n ; i++)
printf(“ %c”, ch[i]); 0 1 2 3 4 5 6
} var h e l l o
void main() Character Array
{ char var[7];
Input
int i;
for(i=0 ; i<5 ; i++) hello
scanf(“ %c”, &var[i]); output
to_print(var, 5);
hello
} 24
Array as a parameter(Cont.)
void to_print(char ch[ ], int n)
{
int i;
for(i=0 ; i<n ; i++)
printf(“ %c”, ch[i]); 0 1 2 3 4 5 6
} var h i
void main() Character Array
{ char var[7];
int i;
var[0] = ‘h’;
output
var[1] = ‘i’;
hi
to_print(var, 2);
} 25
Character Array

#include<stdio.h>
void main()
{
0 1 2 3 4 5 6
char str[7], i; str h e l l o
for(i=0 ; i<5 ; i++)
scanf(“ %c”, &str[i]);
Input

for(i=0 ; i<5 ; i++) hello

printf(“%c”, str[i]); output


} hello
26
Character Array(Cont.)

#include<stdio.h>
void main()
{
0 1 2 3 4 5 6
char str[7], i; str h e l l o
for(i=0 ; i<5 ; i++)
scanf(“ %c”, &str[i]);
Input

for(i=4 ; i>=0 ; i--) hello

printf(“%c”, str[i]); output


} olleh
27
String

String is defined as a NULL terminated character array.

Format Specifier: %s

char str[7];
0 1 2 3 4 5 6
str H e L L o

Character Array

0 1 2 3 4 5 6
str H e L L o \0

String or NULL terminated character array


28
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
scanf(“ %s”, &str); str h e l l o \0
printf(“%s”, str);
}
NULL

Input output

hello hello
29
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
gets(str); str h e l l o \0
puts(str);
}
NULL

Input output

hello hello
30
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
scanf(“ %s”, &str); str h i \0
printf(“%s”, str);
}
NULL

Input output

hi hi
31
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
scanf(“ %s”, &str); str h e l l o \0

for(i=0 ; str[i] != ‘\0’ ; i++)


printf(“%c”, str[i]); NULL

}
Input output

hello hello
32
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
scanf(“ %s”, &str); str h i \0

for(i=0 ; str[i] != NULL ; i++)


printf(“%c”, str[i]); NULL

}
Input output

hi hi
33
String(Cont.)

#include<stdio.h>
void main()
{
char str[7], i; 0 1 2 3 4 5 6
scanf(“ %s”, &str); str r o s e \0

for(i=0 ; str[i] != 0 ; i++)


printf(“%c”, str[i]); NULL

}
Input output

rose rose
34
Exercise

1. Take a string as input and find out its length.


2. Take a string as input and print it in reverse order.
3. Take a string as input and check whether it is palindrome or
not.
4. Take two strings as input and print out the concatenation of
them.
5. Write a program that takes a number as string from user and
then convert it to an integer number.
6. Write a program that takes two large numbers as input and
calculate the sum of the numbers.
Library Functions
String
strlen( )

#include<stdio.h>
#include<string.h>

int main()
{ char str[100];
int len; Input
gets(str); MIST
len = strlen(str);
output
printf(“%d”, len);
4
}
strcpy( )

#include<stdio.h>
#include<string.h>

void main()
{ char str1[100], str2[100]; Input
gets(str1);
hello
gets(str2); world
strcpy(str1, str2);
output

world
puts(str1);
}
strrev( )

#include<stdio.h>
#include<string.h>

void main()
{ char str[100];
gets(str); Input
strrev(str); MIST

output
printf(“%s”, str);
TSIM
}
strcat( )

#include<stdio.h>
#include<string.h>

void main()
{ char str1[100], str2[100];
Input
gets(str1);
Hello
gets(str2); world
strcat(str1, str2);
output

Hello world
printf(“%s”, str1);
}
strcmp( )

#include<stdio.h>
#include<string.h>

void main()
{ char str1[100], str2[100];
Input
int r;
Hello
gets(str1); world
gets(str2);
output
r = strcmp(str1, str2);
-1
printf(“%d”, r);
}
strlen( ) function

int strlen(char str[])


{
int i=0;

while(str[i] != ‘\0’)
{
i++;
}

return i;
}
strcpy( ) function

void strcpy(char str1[], char str2[])


{
int i;

for(i=0 ; str2[i] != ‘\0’ ; i++)


{
str1[i] = str2[i];
}

str1[i] = ‘\0’;
}
strrev( ) function

void strrev(char str[])


{ int i,k;
char t;
k=strlen(str);

for(i=0 ; i<k ; i++, k--)


{
t = str[i];
str[i] = str[k];
str[k] = t;
}
}
Palindrome

• A string is said to be palindrome if the reverse


of the string is the same as the string. For
example, “abba” is a palindrome because the
reverse of “abba” will be equal to “abba” so
both of these strings are equal and are said to
be a palindrome, but “abbc” is not a
palindrome.

46
Palindrome

Using the Standard (simple) Method


• Initialize 2 variables, l from the start and h
from the end.
• now while (h>l), we will check its equivalent
character in the string.
• if it’s not equal then it’s not a palindrome
• else it will traverse half character and the
string is a palindrome.
47
Palindrome
• // C implementation to check if a given
• // string is palindrome or not
• #include <stdio.h>
• #include <string.h>

• int main()
• {
• char str[] = { "abbba" };

• // Start from leftmost and
• // rightmost corners of str
• int l = 0;
• int h = strlen(str) - 1;

• // Keep comparing characters
• // while they are same
• while (h > l) {
• if (str[l++] != str[h--]) {
• printf("%s is not a palindrome\n", str);
• return 0;
• // will return from here
• }
• } 48
Using function in C
Algorithm of is Palindrome(str) function:

• Find the length of str. Let length be n.


• Initialize low and high indexes as 0 and n-1
respectively.
• Do following while low index ‘l’ is smaller than
high index ‘h’.
– If str[l] is not same as str[h], then return false.
– Increment l and decrement h, i.e., do l++ and h–.
• If we reach here, it means we didn’t find a mis
49
Using function in C
Algorithm of isPalindrome(str) function:
• // C program to check if a string is
• // a palindrome or not.
• #include <stdio.h>
• #include <string.h>

• // A function to check if a
• // string str is palindrome
• void isPalindrome(char str[])
• {
• // Start from leftmost and
• // rightmost corners of str
• int l = 0;
• int h = strlen(str) - 1;

• // Keep comparing characters


• // while they are same
• while (h > l)
• {
• if (str[l++] != str[h--])
• {
• printf("%s is not a palindrome\n", str);
• return;
50
• }
Using function in C
Algorithm of isPalindrome(str) function:
• }
• printf("%s is a palindrome\n", str);
• }

• // Driver program to test above function


• int main()
• {
• is Palindrome("abba");
• is Palindrome("abbccbba");
• is Palindrome("geeks");
• return 0;
• }

Output:

abba is a palindrome
abbccbba is a palindrome
geeks is not a palindrome

51
Palindrome

52
Palindrome

53

You might also like