13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 10.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Strings, Pointers and Tools
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings (Continued) Chapter 13
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Strings A string is a sequence of characters treated as a group
Examples Example 1 Demonstrate the use of reference operator
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
CS111 Computer Programming
EECE.2160 ECE Application Programming
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Chapter 8 Character Arrays and Strings
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
CS31 Discussion 1H Fall18: week 6
Characters and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

13. Strings

String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may be extended over more than one line by writing \ immediately followed by the end of the line: printf("Put a disk in drive A, then \ press any key to continue\n"); A string literal may be divided into two or more shorter strings; the compiler will join these together into one string: printf("Put a disk in drive A, then " "press any key to continue\n");

How String Literals Are Stored The string literal "abc" is represented by the three characters a, b, and c, followed by a null character (\0): Like any array, a string literal is represented by a pointer to the first character in the string. A string literal of length 1 is different from a character constant. A string literal of length 1 ("a", for example) is represented by a pointer. A character constant ('a', for example) is represented by an integer value.

How String Literals Are Stored Warning: Don’t ever use a character constant when a string literal is required (or vice-versa). The call printf("\n"); is legal, because printf expects a string as its first parameter, but printf('\n'); is not.

String Variables A string variable is just a one-dimensional array of characters: #define STR_LEN 80 char str[STR_LEN+1]; The array should be one character longer than the string it will hold, to leave space for the null character. Warning: Failure to leave room for the null character may cause unpredictable results when using string-handling functions in the C library.

String Variables A string variable can be initialized: char date1[8] = "June 14"; The date array will have the following appearance: A string initializer need not completely fill the array: char date2[9] = "June 14"; The leftover array elements are filled with null characters:

String Variables If the length of the array is omitted, the compiler will compute it: char date3[] = "June 14"; /* date3 is 8 characters long */

Reading and Writing Strings To read or write a string, use scanf or printf with the %s conversion specification: scanf("%s", str); printf("%s", str); scanf skips white space, then reads characters into str until it encounters a white-space character. No ampersand is needed when using scanf to read into a string variable. Since a string variable is an array, the name of the variable is already a pointer (to the beginning of the array).

Reading and Writing Strings A faster alternative: Use gets and puts instead of scanf and printf: gets(str); puts(str); gets reads characters into str until it encounters a new- line character. puts prints str, followed by a new-line character. scanf and gets automatically put a null character at the end of the input string. printf and puts assume that the output string ends with a null character.

Reading and Writing Strings Warning: Both scanf and gets assume that the string variable is large enough to contain the input string (including the null character at the end). Failing to make the variable long enough will have unpredictable results. To make scanf safer, use the conversion specification %ns, where n specifies the maximum number of characters to be read. Use fgets instead of gets for greater safety.

Accessing the Characters in a String Because of the close relationship between arrays and pointers, strings can be accessed either by array subscripting or by pointer reference. Array version of a function that counts the number of spaces in a string: int count_spaces(const char s[]) { int count, i; count = 0; for (i = 0; s[i] != '\0'; i++) if (s[i] == ' ') count++; return count; }

Accessing the Characters in a String Pointer version of the same function: int count_spaces(const char *s) { int count; count = 0; for (; *s != '\0'; s++) if (*s == ' ') count++; return count; }

Using the C String Library C provides little built-in support for strings. Since strings are treated as arrays, they are restricted in the same ways as arrays—in particular, strings cannot be copied or compared. Warning: Attempts to copy or compare two strings using C’s built-in operators will fail: char str1[10], str2[10]; str1 = str2; /* illegal */ if (str1 == str2)... /* will produce the wrong result */ The C library provides a rich set of functions for performing operations on strings. Declarations for these functions reside in.

The strcpy Function strcpy copies one string into another: char str1[10], str2[10]; strcpy(str1, "abcd"); /* str1 now contains "abcd" */ strcpy(str2, str1); /* str2 now contains "abcd" */ strcpy calls can be chained: strcpy(str2, strcpy(str1, "abcd")); /* both str1 and str2 now contain "abcd" */ Warning: strcpy has no way to check that the second string will fit in the first one.

The strcat Function strcat appends the contents of one string to the end of another: char str[10] = "abc"; strcat(str, "def"); /* str now contains "abcdef" */ Warning: strcat has no way to check that the first string can accommodate the added characters.

The strcmp Function strcmp compares two strings: if (strcmp(str1, str2) < 0)... strcmp returns a value less than, equal to, or greater than 0, depending on whether str1 is less than, equal to, or greater than str2. strcmp considers str1 to be less than str2 if The first i characters of str1 and str2 match, but the (i+1)st character of str1 is less than the (i+1)st character of str2 (for example, "abc" is less than "acc", and "abc" is less than "bcd"), or All characters of str1 match str2, but str1 is shorter than str2 (for example, "abc" is less than "abcd")

The strlen Function strlen returns the length of a string: int i; char str[10]; i = strlen("abc"); /* i is now 3 */ i = strlen(""); /* i is now 0 */ strcpy(str, "abc"); i = strlen(str); /* i is now 3 */ When given an array of characters as its argument, strlen does not measure the length of the array itself; instead, it returns the length of the string stored inside the array.

Writing the strlen Function Version 1: int strlen(const char *s) { int n; for (n = 0; *s != '\0'; s++) n++; return n; } Version 2: int strlen(const char *s) { int n = 0; for (; *s != 0; s++) n++; return n; }

Writing the strlen Function Version 3: int strlen(const char *s) { int n = 0; for (; *s; s++) n++; return n; } Version 4: int strlen(const char *s) { int n = 0; while (*s++) n++; return n; }

Writing the strcat Function char *strcat(char *s1, const char *s2) { char *p = s1; while (*p++); --p; while (*p++ = *s2++); return s1; }

Arrays of strings char planets[][8] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; char planets[][8] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};

Arrays of strings char *planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};

Command line arguments int main(int argc, char *argv[]) { for (i = 1; i < argc; i++) printf("%s\n", argv[i]); } ls -l remind.c