L4-Array and String
L4-Array and String
salary [ 0 ] = 1000;
salary [ 1 ] = 50000;
salary [ 2 ] = 5000;
salary [ 3 ] = salary [ 0 ] + salary [ 2 ] + 1000;
2
Initialization of Arrays
The general form of initialization of arrays is:
number
[0] 20
[1] 30
[2] 0
3
Initialization of Arrays(Continue)
4
A simple Array Program
#include <stdio.h>
#include <conio.h> 10
int main ( ) [0]
n =5
{
int i , a[5] , n; [1] 20
a [ 0 ] = 10;
clrscr ( );
printf ( “ How many numbers\n”); a [ 1 ] = 20; [2] 30
scanf ( “ %d”, & n);
a [ 2 ] = 30;
for ( i = 0; i< n; i ++ )
{ a [ 3 ] = 40; [3] 40
scanf ( “ %d “, & a[ i ]);
a [ 4 ] = 50;
} 50
[4]
printf ( “ Reverse order of the numbers\n” );
sum 100
150
210
100
30
60 = sum 100
150
100
30
60 + X [ 543201 ] 60
10
40
50
20
30
sum = 0;
type array-name[row_size][column_size];
8
Multiplication Table Program
#define ROWS 5
#define COLUMNS 5
void main() {
int row, column, product[ROWS][COLUMNS], i, j ;
printf(“Multiplication table\n\n:”) ;
for(j = 1; j <= COLUMNS; j++)
printf(“%4d”, j);
printf(“\n”);
type array_name[s1][s2][s3]…s[m];
Eg:
1. int survey[3][5][12];
2. float table[5][4][5][3];
10
Passing Arrays to Functions
6/27/20
11
24
Passing Arrays to Functions
6/27/20
12
24
Output
6/27/20
13
24
6/27/202
14
4
Passing Arrays to Functions
6/27/20
15
24
Output
6/27/20
16
24
String
A string is a array of characters.
Any group of characters(except the double quote sign) defined between double
quotation marks is a constant string.
18
DECLARING AND INITIALIZING STRING
VARIABLES
A string variable is any valid C variable name and is always declared as an array.
char string_name[size];
Eg: char city[10];
static char city[9] = {‘N’, ‘E’, ‘W’, ‘ ‘, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};
19
Reading Words
The familiar input function scanf can be used with %s format specification to
read in a string of characters.
main()
OUTPUT
{
char word1[40], word2[40], word3[40], word4[40];
Enter text:
printf(“Enter text:\n”);
scanf(“%s %s”,word1, word2);
Oxford Road, London M17ED
scanf(“%s”, word3);
scanf(“%s”,word4);
Word1 = Oxford
printf(“\n”);
Word2 = Road
printf(“word1 = %s \n word2 = %s \n”,word1, word2);
Word3 = London
printf(“word3 = %s \n word4 = %s \n”,word3, word4);
Word4 = M17ED
}
20
Reading a Line of Text
It is not possible to use scanf function to read a line containing more than one
word.
main()
{
char line[81],character;
int c;
c = 0;
OUTPUT
printf(“Enter text. Press<Return>at end
\n”);
Enter text. Press<Return>at end
do
{
Programming in C is interesting
character = getchar();
line[c] = character;
Programming in C is interesting
c++;
}while(character != ‘\n’);
c = c-1;
line[c] = ‘\0’;
printf(“\n %s \n”,line);
21
}
About String
The C library supports a function that converts a string of digits into their integer
values. The function takes the form
x = atoi(string)
Just as we cannot assign one string to another directly, we cannot join two
strings together by the simple arithmetic addition. That is, the statements such as
string3 = string1 + string2;
string2 = string1 + “hello”;
if(name1 == name2)
if(name == “ABC”);
22
23
strcat( ) Function
The strcat function joins two strings together. It takes the following form
strcat(strcat(string1,string2),string3);
Here three strings are concatenated and the result is stored in string1.
24
strcpy( ) Function
This function works almost like a string assignment operator. It takes the
form
strcpy(string1,string2);
2) strcpy(city1,city2);
25
strlen( ) Function
This function counts and returns the number of characters in a string.
n = strlen(string);
Example:
int x;
x = strlen(“KUET”);
strcmp(string1,string2);
Eg: 1) strcmp(name1,name2);
2) strcmp(name1,”john”;
3) strcmp(“ram”, “rom”);
27
Using gets( )
Limitation of scanf( ) : scanf( ) function can not able to
take space as input.
Example of gets ( )
#include <stdio.h>
int main () {
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str);
return(0);
} 28
Concatenation without strcat( )
#include<stdio.h>
int main(){
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0') {
str1[i]=str2[j];
j++;
i++; }
str1[i]='\0';
printf("\nConcatenated String is %s",str1); return 0;} 29
Program
#include<string.h> OUTPUT
main( ) {
char s1[20],s2[20],s3[20]; Enter two string constants
int x, l1, l2, l3; New York
printf(“Enter two string constants \n”); Strings are not equal
scanf(“%s %s”, s1, s2); s1 = New York length = 7
x = strcmp(s1, s2); characters
if(x != 0) { s2 = York length = 4 characters
printf(“Strings are not equal \n”); s3 = New York length = 7
strcat(s1, s2); } characters
else{
printf(“Strings are equal \n”); } Enter two string constants
strcpy(s3,s1); London London
l1 = strlen(s1); Strings are equal
l2 = strlen(s2); s1 = London length = 6
l3 = strlen(s3); characters
printf(“\ns1 = %s \t length = %d characters \n”,s1, l1); s2 = London length = 6
printf(“\ns2= %s \t length = %d characters \n”,s2, l2); characters
printf(“\ns3 = %s \t length = %d characters \n”,s3, l3); s3 = London length = 6
} characters
30
Thank you
31