0% found this document useful (0 votes)
17 views14 pages

CSE 107 Programming Lecture#10 Array Updated

The document provides an overview of arrays in C, including multi-dimensional arrays, static and dynamic arrays, and character arrays. It explains string handling, including declaration, initialization, reading, writing, and various string manipulation functions such as strcat(), strcmp(), and strcpy(). Additionally, it covers converting character digits to integers and the use of functions like atoi() and strlen().

Uploaded by

loth6523
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)
17 views14 pages

CSE 107 Programming Lecture#10 Array Updated

The document provides an overview of arrays in C, including multi-dimensional arrays, static and dynamic arrays, and character arrays. It explains string handling, including declaration, initialization, reading, writing, and various string manipulation functions such as strcat(), strcmp(), and strcpy(). Additionally, it covers converting character digits to integers and the use of functions like atoi() and strlen().

Uploaded by

loth6523
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

Multi-dimensional array
• C allows three or more dimensions for arrays.
• Example: float result[50][4][5];
result is a three dimensional array; there are 50 students, 4 levels for each
student, and each level has 5 subjects.
result[0][0][0] stores first student’s level 1’s 1st subject’s marks.
Result[2][1][4] stores what??
• ANSI C Does not specify any limits for array dimension; it depends on the
compiler.

CSE 107 1
Static/Dynamic Array
• Static memory allocation: The process of allocating memory at compile time is
called static memory allocation.
Example: int abc;
• Static array: The array which receives static memory allocation is known as static
array. Static arrays are useful if all the data requirements are known in advance.
char Name[20];
• Dynamic memory allocation: The process of allocating memory at run time is
called dynamic memory allocation.
• Dynamic array: The array that is created at run time is known as dynamic array.
Dynamic arrays are created using pointer variables and memory management function malloc(), calloc(),
realloc().
Dynamic arrays are suitable for those cases where the data requirements are not known in advance such as
linked list.

CSE 107 2
Character Arrays and Strings
• String: A string is a collection/sequence of characters treated as a
single data item.
There is no data type for string in C.
Strings are declared as character type arrays.
char string_name[size];
Example: name[20];
 When the compiler assigns a string to a character array, it automatically adds
a null character (‘\0’) at the end of the string. So the size of the string must be
declared to the maximum number of characters in the string plus one.
 The null character is added at the end of a string to indicate the end of a
string. Because the size of the character array which stores the string might be
larger than the size of the string.

CSE 107 3
Declaring and Initializing a String
• Same as numeric array declaration:
char name[20];
• Initialization:
 char name[11]={“Bangladesh”};
 char name[11]={‘B’, ‘a’, ‘n’, ‘g’, ‘l’, ‘a’, ‘d’, ‘e’, ‘s’, ‘h’, ‘\0’};
 char name[]= {‘B’, ‘a’, ‘n’, ‘g’, ‘l’, ‘a’, ‘d’, ‘e’, ‘s’, ‘h’, ‘\0’};
 char name[15]={“Bangladesh”};
B A N G L A D E S h \0 \0 \0 \0
memory
 Do not separate initialization from declaration.
char name[11];
name[11]={“Bangladesh”}; not allowed.
 A string can not be assigned to another string directly.
char a[2]; char b[2];
a[2]=b[2]; not allowed.
a=b; not allowed.

CSE 107 4
Reading a String
• From the terminal:
Using scanf()
char name[20];
scanf(“%s”,name); no need of & before the string name.
A problem with scanf function is that it stops reading input when finds the first white space.
So if the input is “Hajee Danesh”, then the scanf() will read only “Hajee” as input. And the
rest of the memory locations will be filled with garbage values.
Formatted input
scanf(“%ws”,name);
If the width w is equal to or greater than the size of the string, then the entire string will be
assigned to the character array.
If w is less than the size of the string, then the excess characters will be unread.

CSE 107 5
Reading a String
Format specification to read a string with white space: This is known as edit
set conversion code.
scanf(“%[^\n]”,name); In this format, the scanf() will read the whole string “Hajee
Danesh”.
gets() function: This library function reads characters into a string until a
newline character is entered. Then it adds a null character at the end of the
strings.
gets(name);
It allows white spaces.

CSE 107 6
Writing a String
• Using printf() function:
char name[20];
gets(name);
printf(“%s”,name);
• Using puts() function:
puts(name);
• Formatted output:
printf(“%10.4s”,name); displays the first 4 characters of the string (name) in a width of 10 from right justified.
printf(“%-10.4s”,name); displays the first 4 characters of the string (name) in a width of 10 from left justified.

CSE 107 7
Arithmetic Operation on String
• In C, it is allowed to manipulate characters as the same way is done
with numbers.
• Whenever a character is used in an expression, it is automatically
converted in an integer.
• Example: int a;
a=‘a’;
a=a+’z’;
printf(“%d”,a); output: ?

CSE 107 8
Converting character digits into its equivalent
integer values
• Converting a single character:
ASCII value of the ‘character digit’-ASCII value of ‘0’
int convert_to_integer;
convert_to_integer=55-48; (ASCII value of ‘7’-ASCII value of ‘0’)
• Converting a string: A library function atoi converts a string of
character digits to its equivalent integer value.
int convert;
char string[]=“2021”;
convert=atoi(string);

CSE 107 9
String Handling Functions
• Concatenation of two strings
 Two strings can not be joined directly.
char s1[]={“Bangla\0”};
char s2[]={“desh\0”};
char s3[15];
s3=s1+s2; not allowed.
 strcat(): This function joins to strings together. The format is strcat(string1,string2);
 strcat() removes the null character at the end of the first character array (string1)
and adds the second character array (string2) there.
 Example:
strcat(s1,s2);
puts(s1);
strcat(s1,“desh”);
puts(s1);
 Nesting is allowed:
strcat(strcat(s1,s2), “our pride”);

CSE 107 10
String Handling Functions
• Comparing two strings
Two strings can not be compared directly.
if(s1==s2) not allowed.
strcmp(): This function compares two strings. The format is strcmp(string1,string2);
strcmp() compares string1 and string2 and results a zero(0) if both strings are equal;
otherwise results a numeric difference between the first non-matching characters in
the string1 and string2.
Example:
int c;
c=strcmp(s1,s2);
c=strcmp(s1, good”);
if(c==0) printf(“Both strings are equal”);
else printf(“Not equal”);

CSE 107 11
String Handling Functions
• Assigning a string into another
A string cannot be assigned to another directly.
s1=s2; not allowed.
strcpy(): This function copies (assigns) a string into another. The format is
strcpy(string1,string2);
strcpy() assigns string2 into string1.
Example:
strcpy(s1,s2);
puts(s1);
strcpy(s1,“good”);
puts(s1);

CSE 107 12
String Handling Functions
• strlen(): This function counts and results the number of characters in a string. The
format is strlen(string);
Example: int c;
char str[20];
gets(str);
c=strlen(str);
c=strlen(“Desh\0”);
• strncpy(): This function copies (assigns) the left-most n characters from string2
into string1. The format is strncpy(string1,string2,n);
Example: char s1[]={"good\0"};
char s2[]={"bad\0"};
strncpy(s1,s2,2);
puts(s1);

output??

CSE 107 13
String Handling Functions
• strncmp(): This function compares the left-most n characters of string1 and string2 and results the following:
a zero (0) if they are equal, a negative number if the sub-string of string1 is less than string2 and otherwise a
positive number. The format is strncmp(string1,string2,n);
Example: c=strncmp(s1,s2,2);
• strncat(): This function adds the left-most n characters of string2 at the end of string1. The format is
strncat(string1,string2,n);
Example: strncpy(s1,s2,2);
puts(s1);
• strstr(): This function is used to locate a sub-string in a string. The format is strstr(string1,string2);
strstr() searches in string1 to find whether string2 is in string1 or not. If yes, it results the location of the first occurrence of
string2, otherwise a null pointer.
Example: char s1[]={“Bangladesh Bangladesh\0"};
char s2[]={“desh\0"};
int c;
c=strstr(s1,s2);
c=strstr(s1, “desh”);

CSE 107 14

You might also like