Académique Documents
Professionnel Documents
Culture Documents
Announcements
Mid-semester exam at 7:30 AM on 14th September, 2011. Mid semester
Lec-19
Lec-19
Recap
Passing of 2-D arrays to functions g y Strings
Character array with a null character Reading with scanf Writing with printf
Lec-19
Recap: Strings
A string is an array of characters characters. Declaration:
char name [30];
A string is terminated with the null character, \0. String can be initialized in double quotes as:
char place [ ] = IIT Kanpur;
The array place would be allocated 10+1 (11) memory areas. The last area would have \0 null character.
Lec-19
strlen (s)
s is an array of characters Returns the length of string (not counting null character)
strcpy (s, t)
s and t are arrays of characters Copies string t to s, including null character. Note that t = s is not the right way of copying arrays.
Lec-19 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6
Lec-19
String Operations
#include <stdio.h> #include <string.h> int main () { char city [ ] = Delhi; y char place [ ] = IIT; char str [30] = e; printf (%d %d\n, strlen (city), strlen (place)); printf (%s\n, str); strcat (str, city); printf (%s\n, str); strcpy (str, place); py ( , p ); printf (%s\n, str); printf (%d %d\n, strcmp (str, place), strcmp (str, city)); printf (%d\n, strcmp (a b, ab); }
Lec-19 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8
Lec-19
city1 will have 7 memory areas allocated to it. city2 will have 20 memory areas, and only 7 of them initialized, the rest will be 0s.
Lec-19
10
Lec-19
11
Lec-19
12