Vous êtes sur la page 1sur 7

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #19, September 8, 2011

Please switch off your mobile phones.

Announcements
Mid-semester exam at 7:30 AM on 14th September, 2011. Mid semester

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Best Programmers of Lab 4


Section E1 E3 E5 E7 E9 E11 E13 E15 Name Prateek Sahu Rahul Sankhwar Divya Prakash Chintha Sai Teja Reddy Sakshi Sinha Sarthak Chandra Karan Singh Anku Kumar Pandey Section E2 E4 E6 E8 E10 E12 E14 Name Harshad Sawhney Ayush Nigam Ashutosh Verma Eeshit D Vaishnav Dhruv Anand Samyak Daga Amit Munje

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: String input and output


Use %s specification char name[30]; scanf (%s, name); printf (% name); i f (%s, ) Reading will stop as soon as the first white-space character (blank, tab, return) is encountered Notice, no & in front of variable, name. All the characters read are stored in the array starting from 0th element. After last character, an additional array element is used to store \0 character. (So name can only store at most 29 characters.) ( y ) If size of array is larger, rest of the characters are undefined. If size of array is smaller, we will be writing beyond array boundary.

A string is printed up to the null character.


Lec-19 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

In-built C functions for strings


Should include the file string.h string h
#include <string.h>

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

In-built C functions for strings


strcat (s t) (s,
Appends string t to s.

strcmp (s, t) returns the lexicographic comparison between strings s and t.


If they are equal, 0 is returned. If s is later than t, a positive integer is returned. If s i earlier than t a negative integer is returned. is li h t, i i i d Capital letters come earlier than small letters. Blank comes earlier than other letters.

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

String copy function: An implementation


strcpy (s, t) copies string t to s void strcpy (char s [ ], char t [ ]) { int i = 0; while (t [i] != \0) // t is not finished yet { s [i] = t [i]; // copy one character at a time i++; // Note arrays are not called by value } s [i] = \0; // terminate s }

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Array Initialization Examples


char char city1[ ] = {K, a, n, p, u, r, \0}; { K a n p u r \0 }; city2[20 ] = {K, a, n, p, u, r, \0};

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Array Initialization Examples


int int int i t int int int x[ ] = {1, 3, 5}; // 3 members x[10] = {1, 3, 5}; // 3 out of 10 initialized y[4][3] = { {1, 3 5} {2 4 6} {3, 5, 7}, {4 6 8}} [4][3] {1 3, 5}, {2, 4, 6}, {3 5 7} {4, 6, 8}}; // Complete matrix initialized y[4][3] = { {1, 3, 5}, {2, 4, 6}, {3, 5, 7}}; // y[3] row not initialized y[4][3] = {1, 3, 5, 6, 4, 2, 5, 8}; // y[0] y[1], y[2][0], y[2][1] initialized y[0], y[1] y[2][0] y[4][3] = {{1}, {2}, {3}, {4}}; // y[0[0], y[1][0], y[2][0], y[3][0] initialized

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Best of luck for the mid-semester exam

Lec-19

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Vous aimerez peut-être aussi