cse101Lec20.ppt - CSE101-Lec#20 \u2022 Strings \u00a9LPU CSE101 C Programming Outline \u2022 Introduction to string \u2013 Declaration \u2013 Initialization \u2022 Reading

cse101Lec20.ppt - CSE101-Lec#20 u2022 Strings u00a9LPU...

This preview shows page 1 - 9 out of 34 pages.

©LPU CSE101 C Programming CSE101-Lec#20 Strings
Image of page 1
©LPU CSE101 C Programming Outline Introduction to string Declaration Initialization Reading and writing strings functions of the standard input/output library (stdio.h) Processing of strings. String Manipulation Functions from the String Handling Library Comparing strings Determining the length of string
Image of page 2
©LPU CSE101 C Programming Introduction Use of C standard library functions for strings and characters: Easy string and character processing Programs can process characters, strings, lines of text, and blocks of memory These techniques are used to make Word processors Page layout software Typesetting programs
Image of page 3
©LPU CSE101 C Programming Fundamentals of characters Characters Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes 'z' represents the integer value of z (122 ASCII value).
Image of page 4
©LPU CSE101 C Programming Fundamentals of strings Strings Array of characters treated as a single unit called string: Can include letters , digits and special characters (*, /, $) String literal (string constant) - written in double quotes “Lovely Professional University."
Image of page 5
©LPU CSE101 C Programming Fundamentals of strings Strings are arrays of characters String is a pointer to first character (like array) Value of string is the address of first character Each element of the string is stored in a contiguous memory locations. Terminated by a null character (‘\0’) which is automatically inserted by the compiler to indicate the end of string .
Image of page 6
©LPU CSE101 C Programming String Definition They are defined as char array_name[size]; e.g. char carname[30]; or char *carname; It defines an array name and reserves 30 bytes for storing characters and single character consumes 1 bytes each. Since the last byte is used for storing null character so total number of character specified by the user cannot exceed 29 .
Image of page 7
©LPU CSE101 C Programming String Initialization String Initialization Two ways: Define as a character array or a variable of type char * char color[] = "blue"; //char array Or char color[] = { 'b', 'l', 'u', 'e', '\0' }; char *colorPtr = "blue"; //pointer variable Remember that strings represented as character arrays end with '\0' color has 5 elements b l u e \0 color b l u e \0 *colorPtr Temporary
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 34 pages?

  • Fall '14
  • gauravtak
  • ASCII, char array, array S

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture