Introduction to Programming in C - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction to Programming in C

Description:

'atoi' converts C string of digits to corresponding integer value. E.g., int x = atoi('1234' ... string of characters as values of simple type. E.g., string s1, ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 13
Provided by: rohitval
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming in C


1
Introduction to Programming in C
  • Class 19
  • 04/02/2003
  • Rohit Valsakumar

2
C Strings continued
  • Important points to remember
  • The size of the character array used to store a
    string should always be at least one greater than
    the no of characters in the string
  • The string should be terminated by \0 (null a
    back slash followed by zero written together in
    single quotes) character
  • When manipulating these strings be careful never
    to modify the \0 character otherwise the
    computer will crash

3
Using and with C Strings
  • You cannot use and with C strings as they
    are character arrays
  • E.g.,
  • char str110, str2 Hello // str2 legal
  • str1 Good Nite // illegal
  • str2 assignment is legal as it is at point of
    declaration but for str1 using to assign a
    value is not allowed
  • Use this instead strcpy(str1, Good Nite)
  • strcpy() does not check size of char array
    before storing a string into it, so dangerous,
    use strncpy(str1, Good Nite, 9) instead if it
    is provided
  • cannot be used to compare two C strings
    though you wont get an error from the compiler
    if you do so, however the program will behave
    strangely and the operation is not valid either

4
Using and with C Strings
  • Use strcmp(str1, str2) instead returns true if
    the strings do not match otherwise it returns
    false remember this it is not what you would
    expect
  • Strcmp() can also be used for lexicographic
    comparison, it returns 0 if the two strings
    match, -ve no if str1 is lexicographically less
    than str2 and ve no if it is greater than str2
  • include if you want to use strcmp()
    and strcpy()

5
C string input and output
  • cin and cout can be used to read and write a
    C string
  • E.g., - cout
  • char str110 Bon Jour
  • cout
  • Bon Jour will appear on the monitor
  • For reading with cin take care to notice that
    all white spaces are skipped, moreover each
    reading of input stops at the next space or line
    break
  • E.g.,
  • char str222, str322
  • cin str2 str3
  • cout
  • User types Bon Jour Mademoiselle
  • BonJour End - this is what appear on the
    monitor

6
C string input and output
  • This is not what we expected so use
    cin.getline(str2, 22) instead
  • If size of char array is smaller than line then
    only that many characters will be read as will
    fit in the array, the \0 character is
    automatically inserted by cin.getline()
  • If the char array is greater than the line to be
    read in then the reading will stop when the line
    is read
  • Try all combinations yourself it is good practice

7
C string to number conversions
  • include
  • atoi converts C string of digits to
    corresponding integer value
  • E.g.,
  • int x atoi(1234)
  • cout
  • 1234 - printed on monitor
  • atol converts strings to long value
  • atof converts strings to double value
  • If the conversion is not possible then the above
    functions return zero

8
The standard string class
  • include
  • Allows you to treat a string of characters as
    values of simple type
  • E.g.,
  • string s1, s2, s3
  • s1 Bon
  • s2 Jour
  • s3 Mademoiselle
  • string s4 s1 s2 s3
  • cout
  • Output will be
  • BonJourMademoiselle
  • Bon Jour Mademoiselle

9
The standard string class
  • You can use the , , , and many other
    operators with objects of class string
  • You can also use cout and cin to write and
    read values into objects of type string
  • With cin you have to take the same care that
    you take in the case of character arrays if you
    want to read in white spaces i.e., use a function
    getline() as shown below
  • string line
  • cout
  • getline(cin, line)
  • cout
  • With objects of class string we dont have to
    worry about size limitations or the null
    character either

10
String Processing with the Class string
  • All the operators i.e., , , !, ,
    work in the same manner as when used with values
    of simple types
  • is used to join to strings in the order they
    are written
  • can also be used to concatenate one string
    to the other
  • E.g.,
  • string s1 Au, s2 revoir
  • s1 s2
  • cout s1
  • Output
  • Au revoir

11
String Processing with the Class string
  • We can access individual characters of the string
    by indexing
  • cout
  • Output
  • e
  • - , , /, , - etc. cannot be used
    with objects of class string
  • Read pages 674, 675, 676, 677 of the textbook

12
Converting between string and C Strings
  • We can copy a C string to a string object but not
    vice versa
  • char cstr Good day //legal
  • string str cstr, str1 // legal
  • str1 cstr // legal
  • cstr str // illegal
  • Strcpy(cstr, str) //illegal
  • Explicit type casting is needed if you want to
    copy an object of string class into a C string
  • This is done by stringc_str() member function
    of the string class
  • Thus,
  • strcpy(cstr, str.c_str()) // legal
  • Will work
Write a Comment
User Comments (0)
About PowerShow.com