Introduction to C/C++ Lecture 5 - String & its Manipulation Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur rsehgal@cse.iitk.ac.in nishitm@cse.iitk.ac.in May 30, 2016 Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 1 / 11
Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11
Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11
Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. char a string a takes one byte takes 2 bytes Here a is single character value while a is string with two characters, the first one is a and second one is \0. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11
Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. char a string a takes one byte takes 2 bytes Here a is single character value while a is string with two characters, the first one is a and second one is \0. Declaration of string: char string name[string size]; Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11
String Input Use %s field specification in scanf to read string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11
String Input Use %s field specification in scanf to read string. Ignores leading white spaces. reads characters until next white space encountered. Can use the width value in the field specification to limit the number of characters read: example: scanf( %10s,string); \\It only reads 10 characters Here width of string array should be one more than the defined size in scanf ( i.e char string[11] ) Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11
String Input Use %s field specification in scanf to read string. Ignores leading white spaces. reads characters until next white space encountered. Can use the width value in the field specification to limit the number of characters read: example: scanf( %10s,string); \\It only reads 10 characters Here width of string array should be one more than the defined size in scanf ( i.e char string[11] ) scanf requires address of the variable, identifier (pointer) is address itself. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11
Contd.. scan set restrict the scanning input string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11
Contd.. scan set restrict the scanning input string. Doesn t ignores leading white spaces. Characters read as long as character falls in scan set. example: scanf( %[0123456789],number); \\It only reads numbers Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11
Contd.. scan set restrict the scanning input string. Doesn t ignores leading white spaces. Characters read as long as character falls in scan set. example: scanf( %[0123456789],number); \\It only reads numbers Putting at the start to negate the set example: scanf( %[ \n],string); \\read until new line character encountered. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11
String Output Use %s field specification in printf. Characters in string printed until \0 encountered. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 5 / 11
Main operations on String strcpy strcat strcmp strlen copy one string into another append one string onto the right side of the other compare alphabetic order of two strings return the length of a string Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 6 / 11
strcpy and strncpy Syntax of strcpy strcpy ( destination string, source string ) Copies the characters (including the \0) from the source string to the destination string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 7 / 11
strcpy and strncpy Syntax of strcpy strcpy ( destination string, source string ) Copies the characters (including the \0) from the source string to the destination string. Syntax of strncpy strncpy ( destination string, source string, n ) Same as the strcpy, but the copy stops after n characters. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 7 / 11
strcat Syntax strcat ( destination string, source string ) Appends source string to right hand side of destination string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 8 / 11
strcmp Syntax strcmp ( string1, string2 ) Compares string1 and string2 alphabetically. Return a value <0 if string1 precedes string2 ( i.e string1 <string2 ) Return a value >0 if string2 precedes string1 ( i.e string1 >string2 ) Return 0 if string1 equals to string2 Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 9 / 11
strcmp Syntax strcmp ( string1, string2 ) Compares string1 and string2 alphabetically. Return a value <0 if string1 precedes string2 ( i.e string1 <string2 ) Return a value >0 if string2 precedes string1 ( i.e string1 >string2 ) Return 0 if string1 equals to string2 Note: lowercase characters are greater than Uppercase Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 9 / 11
strlen Syntax strlen ( string ) Returns length of the string excluding null character. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 10 / 11
The End Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 11 / 11